Question

java: How can I change this from a do-while to an if/else or while int result;...

java:

How can I change this from a do-while to an if/else or while

int result;
do {
result = rollTwoDice();
} while (result != point && result != 7);

if (result == 7) {
System.out.println("You lose");
loss++;
} else {
System.out.println("You win");
win++;

}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The modified code using while loop and if/else is:- (Modified part is in bold)

int result;
while(1) {
result = rollTwoDice();
if(result != point && result != 7)
continue;
else
break;

}

if (result == 7) {
System.out.println("You lose");
loss++;
} else {
System.out.println("You win");
win++;

}

Add a comment
Know the answer?
Add Answer to:
java: How can I change this from a do-while to an if/else or while int result;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java i know that result will be 415. Can you explain how it works. step by...

    Java i know that result will be 415. Can you explain how it works. step by step please. thanks public static void whatsPrinted(int A[]) { for (int i=1; i<A.length; i++) { A[i]=A[i-1]*2+1; } } public static void main(String args[]) { int A[] = {12,3,8,9,7,11}; whatsPrinted(A); System.out.println(A[A.length-1]); }

  • Write Java code using a for loop that is equivalent to the following do-while loop: int...

    Write Java code using a for loop that is equivalent to the following do-while loop: int i = 2 ; do { System.out.println(i + " squared is " + (i * i)) ; i += 2 ; } while (i <= 10) ;

  • Java programming How do i change this program to scan data from file and find the...

    Java programming How do i change this program to scan data from file and find the sum of the valid entry and count vaild and invalid entries The provided data file contains entries in the form ABCDE BB That is - a value ABCDE, followed by the base BB. Process this file, convert each to decimal (base 10) and determine the sum of the decimal (base 10) values. Reject any entry that is invalid. Report the sum of the values...

  • Q1: Which of the following is a double-selection control statement? do…while for if…else if. Q2: Which...

    Q1: Which of the following is a double-selection control statement? do…while for if…else if. Q2: Which of the following is not a Java keyword? do next while for Q3: What is output by the following Java code segment? int temp; temp = 200; if ( temp > 90 )    System.out.println( "This porridge is too hot." ); if ( temp < 70 )    System.out.println( "This porridge is too cold." ); if ( temp == 80 )    System.out.println( "This...

  • This is a java code but I need to understand the logic of how is going...

    This is a java code but I need to understand the logic of how is going to run. Can someone help please. int i = 1; while (i <= 5) { xMethod(i); i++; } System.out.println("i is " + i); } public static void xMethod(int i) { do { if (i % 2 != 0) System.out.print(i + " "); i--; } while (i >= 1); System.out.println();

  • Java, how would i do this public static void main(String[] args) { int n = 3;...

    Java, how would i do this public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...

  • this is in java program, I need to know how can I solve this in hand...

    this is in java program, I need to know how can I solve this in hand with out using computer program.   public class JavaApplicationGuess { public static void main(String[] args) { int i = 0; int x = 0;    for (i =3; i > 1; i--) { do { x = x * 2 + 3; System.out.println(x); } while (x < 8); x = x -1 + i; System.out.println(i + " " + x); } } } my output...

  • Can you help me on this programming homework? Can you do same like this? Just do...

    Can you help me on this programming homework? Can you do same like this? Just do the basic java...for FOR,WHILE and DO-WHILE LOOP. I attached the problem... thank you actor Navigate Search Project Win package csci2e73 public class repetition public static void stringti arss) TODO Auto-Benerated method stub int result for (int i 51 i 10e: i i s) result result iu System-out printf Tsun is result): System-out printf Vnsum frem method calli sum(100) public static int sus (int n)...

  • Having trouble with the do while/while loop and the switch statement. I got some of the...

    Having trouble with the do while/while loop and the switch statement. I got some of the switch statement but cant get the program to repeat itself like it should.What i have so far for my code is below. Any help is appreciated... i am not sure what I am doing wrong or what i am missing. I am completely lost on the while loop and where and how to use it in this scenario. import java.util.Scanner; public class sampleforchegg {...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT