Results 1 to 2 of 2
- 09-28-2011, 06:34 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
Subsitution for while loop and text input
I have a problem 1 i want to hit countdown the days till the end of the month and when done have a message to say that the program ends and i want text input so i can have a yes and no question heres the code, and sorry for the weird varibles i was feeling silly.
import java.util.Scanner;
class DERP_CLASS_2{
public static void main(String args[]){
Scanner water = new Scanner(System.in);
double loan = 0;
double saving = 0;
double days;
double loan2;
double money = 0;
double loan1 = 0;
double saving1 = 2;
double loan3 = 0;
System.out.println("How many days?");
days = water.nextDouble();
while (days <= 1);
{
--days;
String input;
System.out.println("How much money?");
loan1 = water.nextDouble();
System.out.println("Is marley saving?");
String cat = water.next();
if(cat == "yes"){
saving += saving1;
}
loan2 = loan1;
loan3 = loan1;
while (loan1 > 9.99){
loan1 -= 10;
++loan2;
++money;
++loan3;
}
System.out.print("You owe:");
System.out.println(loan3);
System.out.print("Interest:");
System.out.println(money);
System.out.print("Amount loaned:");
System.out.println(loan2 - money);
System.out.print("Extra:" + saving);
}
}
}
- 09-28-2011, 07:19 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: Subsitution for while loop and text input
Please use code tags when posting.
Firstly, the semicolon after the while statement means that the block that follows it is in no way associated with the while condition. Secondly, if you were to remove this semicolon then, assuming days <= 1 to begin with, it will never stop looping. (If it was an int, then eventually it would wrap around to 0x7FFFFFFF and evaluate to false, but as a double it'll get to the stage where the exponent is too large for subtracting 1 to make any difference to it.)Java Code:while (days <= 1); { --days;
Don't compare strings with ==. This compares object references, not string content. Use .equals() instead.Java Code:String cat = water.next(); if(cat == "yes"){
Similar Threads
-
help me for this nested for loop with users input
By jakemanalaotao in forum New To JavaReplies: 4Last Post: 09-06-2011, 03:50 PM -
Loop returns at second input rather than first.
By RockW in forum New To JavaReplies: 2Last Post: 05-05-2011, 06:03 AM -
while loop bypasses scanner input on 2nd pass
By xf021209 in forum New To JavaReplies: 2Last Post: 02-28-2010, 08:10 AM -
[SOLVED] User Input - loop
By new person in forum New To JavaReplies: 4Last Post: 02-22-2009, 10:02 PM -
loop when there is no user-input
By becky in forum New To JavaReplies: 12Last Post: 02-02-2009, 10:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks