Not sure how to go about this...
So I'm writing a code to ask the user how many weeks they go till they get paid, how many hours they worked each day, how much their pay rate is, and then compile it all. Only problem is that I can't get the numbers to add up right. I'm sure I haven't written the code properly since I'm new to this and self taught but maybe someone can help? When the program runs a second time (if they work 2 weeks) it over wrights the data stored in the variables.
Code :
import java.util.Scanner;
public class paycheck1 {
public static void main(String[] args) {
double sunday = 0;
double monday = 0;
double tuesday = 0;
double wednesday = 0;
double thursday = 0;
double friday = 0;
double saturday = 0;
System.out.println("How many weeks per paycheck?");
Scanner week = new Scanner (System.in);
int weekc = week.nextInt();
int weekv = weekc;
weekv++;
int count = 1;
while (count != weekv)
{
// check System.out.println ("count: " + count + "weekv: " + weekv);
System.out.println ("How many hours did you work Sunday of week " + count + " ?");
Scanner scan = new Scanner (System.in);
sunday = scan.nextDouble();
System.out.println ("How many hours did you work Monday of week " + count + " ?");
Scanner scan1 = new Scanner (System.in);
monday = scan1.nextDouble();
System.out.println ("How many hours did you work Tuesday of week " + count + " ?");
Scanner scan2 = new Scanner (System.in);
tuesday = scan2.nextDouble();
System.out.println ("How many hours did you work Wednesday of week " + count + " ?");
Scanner scan3 = new Scanner (System.in);
wednesday = scan3.nextDouble();
System.out.println ("How many hours did you work Thursday of week " + count + " ?");
Scanner scan4 = new Scanner (System.in);
thursday = scan4.nextDouble();
System.out.println ("How many hours did you work Friday of week " + count + " ?");
Scanner scan5 = new Scanner (System.in);
friday = scan5.nextDouble();
System.out.println ("How many hours did you work Saturday of week " + count + " ?");
Scanner scan6 = new Scanner (System.in);
saturday = scan6.nextDouble();
count ++;
}
double total = sunday + monday + tuesday + wednesday + thursday + friday + saturday;
System.out.println ("In total, you worked " + total + " hours." );
System.out.println ("What is your hourly pay?");
Scanner scan7 = new Scanner (System.in);
double pay = scan7.nextDouble();
double paytotal = pay*total;
System.out.println ("Your paycheck for this period of " + weekc + " weeks will be $" + paytotal +".");
}
}
Re: Not sure how to go about this...
A very simple solution seems to be if you keep a sum.
sunday += scan.nextDouble();
In fact you could keep only a single variable total and instead of the days of the week use
total += scan.nextDouble();
Re: Not sure how to go about this...
How do I print out the Java code in the box like the OP has?
Re: Not sure how to go about this...
[highlight=java] Code goes here [/highlight]
Re: Not sure how to go about this...
Quote:
Originally Posted by
javapenguin
[highlight=java] Code goes here [/highlight]
Thanks for that
Re: Not sure how to go about this...
thanks so much ilan :3
I'll try it out asap :D
Re: Not sure how to go about this...
You don't have to create a new Scanner object every time, just reuse one.
Here's an example
Code Java:
Scanner in = new Scanner(System.in);
System.out.println("Say something");
in.readLine();
System.out.println("Say another thing");
in.readLine();
System.out.println("Say a number");
in.readInt();