I am trying to input 3 different variables in an array inside the loop at a time, until I stop Not for any variable while the loop only allows me to input the value of a second variable if the first variable does not turn off, and so on with a third variable value input
now, first The loop gets corrected and I can input all 3 variables, but 2 and 3 times, the first variable for the loop Output, but I'm not allowed to input the input before leaving the second variable. What I am saying:
-
Name: afasdf
-
Additional information: afdsaf
-
Unit cost: 123123214
-
Name: Additional Information: adflskjflk also does not eliminate the stop loop
-
Unit cost: 123217
I know that this loop only works when a variable works, and I Trying to use a loop instead of a loop, and adding other tonnage statements, but it looks like
What about my break There is something wrong with the installation of s?
Thank you very much
The way I establish the last breaker (which is also one, when I stop for double variables) here is my code
ArrayItem s = new ArrayItem (); String name = null; id = zero; Double money = 0; Boolean breaker = false; While (breaker == incorrect) {System.out.print ("name:" + "\ t"); Name = input NXtine (); If (name.equals ("Stop") // See that program breaker = true must be stopped; System.out.print ("Additional Information:" + "\ t"); Description = input NXtine (); If (id.egeil ("stop") breaker = true; System.out.print ("Unit cost:" + "\ t"); Money = input Nxt double (); // I think if I switch the input off / whenever the variable looks double (input.quilues ("stop")) || Input ("stop")) breaker = true; Else s.SetNames (name); S.SetInfo (description); S.SetCost (Money); }
A few things about code:
"name:" "\ T " can be simplified OT
" name: \ t ". This is true for the rest of the code in Java, it is customary to use CamelCase where the first word is lowercase. For example,
s.SetMoney will be
s.setMoney . In addition, variables follow the same rule where
money will be and
id will be, and id will be
id . If your teacher is teaching you otherwise, follow their style.
The loop should also be a do-all loop:
do {// read each value in sequence, and then check to see if you // Should be stopped / you can simplify it in a function which gives the object / return / if the value of the return value should be stopped (the capital requirement for D / double for the return type is required) Cause * /) {break; } S.setNames (name); S.setId (id); S.setMoney (money); } While (true); Private string getString (scanner input) {string result = input.nextLine (); // find STOP if (result.equalsIgnoreCase ("stop")) {result = zero; } Return results; } Private double getDouble (scanner input) {double result = zero; // line read string string = getstring (input) is a string in search of; // tap if it prevents (line! = Null) {try {result = double.parseDouble (line); } Hold (NumberFormatException E) {// is not a valid number, but do not close either! }} Return result; }
There are a number of concepts, but they should help you progress. I'll put you the pieces together.
In addition, you need to fix brackets, but this is not the only issue. Because
currency is a
double , you will have to read the value as a
string I suspect that
input a < Code> scanner object, so you can check
Input.hasNextDouble () if it is not, you can conditionally view
string For whether this is a "stop" (Note: You are checking "Stop" and "Stop", which are not identical). Your last, non-spot probe compares the "stop" to the scanner, which will never be true. Check
System.out.print ("Unit cost: \ t"); If (Input.hasNextDouble ()) {Money = Input.Next Double (); // Now you can set your object // ...} // This is not a double; Look for "pause" if (input .nxtine) .all undiscovered cass ("stop")) // // output loop break; } // Note: If this is not a double or stop, then you have not left // and you have not set a fund
Comments
Post a Comment