473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How do I loop this?

12
Here is my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     System.out.println("Please enter the number you think the die will roll. The number must be between 1 and 6");
  3.     for (boolean numRun=true; numRun;) {
  4.             Scanner sc = new Scanner(System.in);
  5.             if (sc.hasNext()) {
  6.                 numInput = sc.nextInt();
  7.                 if (numInput >= 1 && numInput <= 6) {
  8.                     numRun= false;
  9.                     System.out.println("The number you have entered is between 1 and 6");
  10.                 } else {
  11.                     System.err.println("You have entered an incorrect value.");
  12.                     numRun=true;
  13.  
  14.                 }
  15.             }
  16.     }
How do i write the code so that it keeps asking the same questions until the value the user entered is in the 1 to 6 parameters?
Jan 26 '11 #1
6 1614
Rabbit
12,516 Expert Mod 8TB
Use a while loop instead of a for loop.
Jan 26 '11 #2
Crosson
12
okay i changed the code a little bit:
Expand|Select|Wrap|Line Numbers
  1.     System.out.println("Please enter the number you think the die will roll. The number must be between 1 and 6");
  2.     for (boolean numRun=true; numRun;) {
  3.         Scanner sc = new Scanner(System.in);
  4.  
  5.         int numInput = sc.nextInt();
  6.         if(numInput >= 1 && numInput <= 6) {
  7.             int number = numInput;
  8.             numRun = false;
  9.         } else {
  10.             System.out.println("The # you entered is not between 1 and 6. Please re-enter a number.");
  11.             numRun = true;
  12.         }
  13.     }
  14.     System.out.println(number);
  15.  
And I think the loop will work fine, but what my problem is that the variable number cannot be found outside of the loop. How do i get the variable outside the loop?
Jan 26 '11 #3
Rabbit
12,516 Expert Mod 8TB
Declare the variable outside the loop
Jan 26 '11 #4
Crosson
12
i declared the variable 'number' outside the loop ... but now my program is saying the the variable might not have been initialized
Jan 26 '11 #5
Rabbit
12,516 Expert Mod 8TB
Initialize it to 0
Jan 26 '11 #6
Crosson
12
thanks for the help !
Jan 26 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jo Vermeulen | last post by:
Hello, I was wondering if it's possible to add a condition to the enhanced for loop. This is an example of an enhanced for loop: LinkedList<MyClass> children = new LinkedList<MyClass>();...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
23
by: philipl | last post by:
hi, I have some code here which basically look for within a string, the occurance of any 3 consectative characters which are the same. so AAA bbb etc would be reported by this function. I later...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
1
by: pauljturner99 | last post by:
Hi, I'm trying to pass a parameter from a for loop to the nested while loop but only the first counter is passed. Here is the code: dim ctr redim ctr(5) ctr(0) = 2 ctr(1) = 4 ctr(2) = 6
3
by: Trev | last post by:
Hi, I'm learning C# and trying to write the program below, everything works fine until the last section: Console.Write("Would you like to play again y/n: "); again = Console.ReadLine(); if...
8
by: malkarouri | last post by:
Hi everyone, I have an algorithm in which I need to use a loop over a queue on which I push values within the loop, sort of: while not(q.empty()): x = q.get() #process x to get zero or more...
5
by: =?Utf-8?B?aUhhdkFRdWVzdGlvbg==?= | last post by:
I have a textbox control in each row of the table and I need to display it dynamically In the first row of the table it is txtSample1 and in the second row it is txtSample2 The below code is...
3
by: =?Utf-8?B?aUhhdkFRdWVzdGlvbg==?= | last post by:
I actually need to do the below for 10 controls ddlInsuranceCompany1.SelectedItem.Text = Value; txtPolicy1.Text = value; SO i opted for a for loop My question is how do i add contol +...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.