java重复循环,java循环重复程序

I am extremely new to java I am in my second week in classes or so--

I need my program to keep going or exit according to the user. It is a payroll calculation and I want the end to say "Do you want to continue (y/n)" I want Y to repeat my entire program of questions and no to end program. I am using Jgrasp and I am very very new. I am assuming it needs a loop and I am not totally sure, I just got this to run and compile correctly-- it runs correctly for me so it is a good start and I am hoping to get help on how to do this as I am seeing a ton of different ways and different programs for it. thanks.

import java.util.Scanner;

public class calculations {

public static void main(String [] args) {

Scanner reader = new Scanner(System.in);

Scanner in = new Scanner(System.in);

double Regpay;

double Payperhour;

int HoursAweek;

double Pay;

double OvertimeHours;

double OvertimePay;

double Dependants;

double SocSecTax;

double FederalTax;

double StateTax;

int UnionDues;

double AllTaxes;

double FinalPay;

String playAgain;

System.out.print("Enter your pay per hour:");

Payperhour = reader.nextDouble ();

System.out.print("Enter your regular Hours a week:");

HoursAweek = reader.nextInt();

System.out.print("Enter your overtime hours:");

OvertimeHours = reader.nextDouble();

Regpay = Payperhour * HoursAweek;

OvertimePay = OvertimeHours * 1.5 * Payperhour;

Pay = OvertimePay + Regpay;

SocSecTax = Pay * .06;

FederalTax = Pay * .14;

StateTax = Pay * .05;

UnionDues = 10;

AllTaxes = SocSecTax + FederalTax + StateTax + UnionDues;

FinalPay = Pay -= AllTaxes;

System.out.println("Your pay this week will be " +FinalPay);

{

System.out.println("How many Dependants:");

Dependants = reader.nextInt();

if (Dependants >= 3) {

Dependants = Pay + 35;

System.out.println("Your Pay is:" +Dependants);

} else if(Dependants < 3) {

System.out.println("Your Pay is:" +Pay);

}

}

}

}

解决方案

Here is the basic idea with your code:

import java.util.Scanner;

public class calculations{

public static void main(String [] args) {

Scanner reader = new Scanner(System.in);

Scanner in = new Scanner(System.in);

double Regpay;

double Payperhour;

int HoursAweek;

double Pay;

double OvertimeHours;

double OvertimePay;

double Dependants;

double SocSecTax;

double FederalTax;

double StateTax;

int UnionDues;

double AllTaxes;

double FinalPay;

String playAgain;

int runAgain = 1;

while (runAgain == 1) {

System.out.print("Enter your pay per hour:");

Payperhour = reader.nextDouble();

System.out.print("Enter your regular Hours a week:");

HoursAweek = reader.nextInt();

System.out.print("Enter your overtime hours:");

OvertimeHours = reader.nextDouble();

Regpay = Payperhour * HoursAweek;

OvertimePay = OvertimeHours * 1.5 * Payperhour;

Pay = OvertimePay + Regpay;

SocSecTax = Pay * .06;

FederalTax = Pay * .14;

StateTax = Pay * .05;

UnionDues = 10;

AllTaxes = SocSecTax + FederalTax + StateTax + UnionDues;

FinalPay = Pay -= AllTaxes;

System.out.println("Your pay this week will be " + FinalPay);

{

System.out.println("How many Dependants:");

Dependants = reader.nextInt();

if (Dependants >= 3) {

Dependants = Pay + 35;

System.out.println("Your Pay is:" + Dependants);

} else if (Dependants < 3) {

System.out.println("Your Pay is:" + Pay);

}

}

System.out.println("Again??? Press 1 to run again and 0 to exit");

runAgain = reader.nextInt();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值