How do I loop through a string in R?

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

Was this solution helpful to you?

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:

Just Added Q & A:

Find solution

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.