954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

die roll repeat

so all i want to do is roll 1 die repeatedly and add it to a total until the total is equal to or greater than 31
this is what i have got to so far

import java.io.*;
import java.util.*;

public class comp {
    public static void main(String[] args)
        throws FileNotFoundException {
       
        Scanner user = new Scanner(System.in); 
		   Random r = new Random();
 	

		int roll;	
		int comptotal = 0;
	
	do{         
			roll = r.nextInt(6) + 1;
			comptotal = comptotal + roll;
	}while (comptotal >= 31);
        System.out.println(comptotal);
}}


it only rolls the die once i can't think of another way to roll the die again and keep rolling it until the total is equal to or greater than 31

pirateninja1111
Newbie Poster
14 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

You have your condition in the while backwards. You need to say that comptotal < 31 because it comes from 0 and adds until it equals or passes 31.

so all i want to do is roll 1 die repeatedly and add it to a total until the total is equal to or greater than 31 this is what i have got to so far

import java.io.*;
import java.util.*;

public class comp {
    public static void main(String[] args)
        throws FileNotFoundException {
       
        Scanner user = new Scanner(System.in); 
		   Random r = new Random();
 	

		int roll;	
		int comptotal = 0;
	
	do{         
			roll = r.nextInt(6) + 1;
			comptotal = comptotal + roll;
	}while (comptotal >= 31);
        System.out.println(comptotal);
}}

it only rolls the die once i can't think of another way to roll the die again and keep rolling it until the total is equal to or greater than 31

Sephladaj
Newbie Poster
1 post since Apr 2010
Reputation Points: 6
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You