Need to implement loop in this string?
-
I'm trying to make it allow more than one chance to input an answer. This is abbreviated code for the sake of space. Any help is appreciated. :-) I could make this a lot prettier if I knew how to preformat text on here but I can't figure it out. if you know let me know lol. import java.util.Random; import java.util.Scanner; import static java.lang.System.out; class randomNumber { public static void main(String args[]) { Random myRandom = new Random(); String reply; int randomNumber; randomNumber = myRandom.nextInt(2) + 1; switch (randomNumber) { case 1 : out.println("1 = "); Scanner myScanner = new Scanner(System.in); reply = myScanner.nextLine(); if (reply.equalsIgnoreCase("A")) { System.out.println("Correct!"); } else { System.out.println("Try again!"); } break; case 2 : out.println("2 = "); Scanner myScanner1 = new Scanner(System.in); reply = myScanner1.nextLine(); if (reply.equalsIgnoreCase("B")) { System.out.println("Correct!"); } else { System.out.println("Try again!"); } } } }
-
Answer:
Use this while loop before the switch statement: while (!reply.equalsIgnoreCase("A") && !reply.equalsIgnoreCase("B")) switch..... Or better yet, do a do while loop: do { switch..... ..... } while (!reply.equalsIgnoreCase("A") && !reply.equalsIgnoreCase("B"))
Ezekiel at Yahoo! Answers Visit the source
Other answers
Sorry. I did not understand what really you want. If you want the game to run more than once then put in a loop import static java.lang.System.out; class randomNumber { public static void main(String args[]) { Random myRandom = new Random(); int Ntrial=myRandom.nextInt()%11; // ten chances while(--Ntrials !=0) <------------New loop added { String reply; int randomNumber; randomNumber = myRandom.nextInt(2) + 1; switch (randomNumber) { case 1 : out.println("1 = "); Scanner myScanner = new Scanner(System.in); reply = myScanner.nextLine(); if (reply.equalsIgnoreCase("A")) { System.out.println("Correct!"); } else { System.out.println("Try again!"); } break; case 2 : out.println("2 = "); Scanner myScanner1 = new Scanner(System.in); reply = myScanner1.nextLine(); if (reply.equalsIgnoreCase("B")) { System.out.println("Correct!"); } else { System.out.println("Try again!"); } break; } } } }
James Bond
Related Q & A:
- How To Recharge Loop Mobile?Best solution by rechargeitnow.com
- how to process a simple loop in WWW::Mechanize to be more efficient?Best solution by stackoverflow.com
- How do I loop through many files in one folder?Best solution by thespreadsheetguru.com
- How to see if one string contains another string?Best solution by Stack Overflow
- What is the difference between a for loop, a do while loop and a do loop?Best solution by wiki.answers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.