can some one tell me why this isn\'t working import java.util.Scanner; public cl
ID: 665059 • Letter: C
Question
can some one tell me why this isn't working
import java.util.Scanner;
public class payroll {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Payroll: ");
System.out.print("Enter the company's name");
String compName = in.nextLine();
System.out.print("Enter tody's date: ");
String date=in.nextLine();
System.out.print("Enter the number of employees: ");
int numEmplo = in.nextInt();
System.out.println();
int i;
for (i=0;i<3;i++)
{
System.out.println("Enter the first name and last name of the employee: ");
String last =in.nextLine();
String first=in.nextLine();
System.out.println("Enter the Employee pay rate: ");
double payRate=in.nextDouble();
while(payRate>75)
{
System.out.println("This employee dose not make that much");
System.out.println("Enter the employee pay rate");
if(payRate<0)
{
System.out.println("Try again! Pay rate can not be neagitive!");
System.out.println("Enter the employee pay rate");
}
}
System.out.println("Enter the hours the employee worked: ");
double workedHour=in.nextDouble();
while(workedHour>72)
{
System.out.println("Too many hours employee worked: ");
System.out.println("Enter the hours the employee worked ");
if (workedHour<0)
{
System.out.println("Try again! the hours emloyee worked can not be neagitive!");
System.out.println("Enter the employee hours worked");
}
}
double pay = workedHour*payRate;
System.out.println("" +date +compName +first +last +pay);
}
}
}
====================================================================================
import java.util.Scanner;
public class lottery {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
try
{
int lottery= (int)(Math.random()*1000);
System.out.println("Enter your lottery pick (three digits): " );
int guess = in.nextInt();
int lottDigit1=lottery/10;
int lottDigit2=lottery %10;
int lottDigit3=lottery +10;
int guess1= guess/10;
int guess2= guess%10;
int guess3= guess + 10;
char choice = 'Y';
while (choice != 'N'&& choice !='n')
{
System.out.println("The lottery number is " + lottery);
if (guess == lottery)
System.out.println("You won $10,00");
else if (guess3==lottDigit1
&&guess2==lottDigit3
&& guess1 == lottDigit2)
System.out.println("You match all digits: You won $3,000");
else if (guess1==lottDigit1
|| guess1==lottDigit2
|| guess1== lottDigit3
|| guess2 == lottDigit1
|| guess2 == lottDigit2
|| guess2 == lottDigit3
|| guess3 == lottDigit1
|| guess3 == lottDigit2
|| guess3 == lottDigit3)
System.out.println("Match one digit: you win 1,000");
else
System.out.println("HAHA you didnt win!! Since you didn't win do you want to try again? Press Y to continue and N to end:");
choice = keyboard.next().charAt(0);
}
}
catch (Exception e)
{
System.out.println("ERROR");
}
}
}
Explanation / Answer
//Changes are in bold
//payroll.java
import java.util.Scanner;
public class payroll
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Payroll: ");
System.out.print("Enter the company's name");
String compName = in.nextLine();
System.out.print("Enter tody's date: ");
String date=in.nextLine();
System.out.print("Enter the number of employees: ");
int numEmplo = in.nextInt();
System.out.println();
int i;
//set numEmplo as condtion for loop to read
for (i=0;i<numEmplo;i++)
{
System.out.println("Enter the first name and last name of the employee: ");
//User next() method instead of nextLine method then it takes
//first name and last name separetely from user .
String first =in.next();
String last=in.next();
System.out.println("Enter the Employee pay rate: ");
double payRate=in.nextDouble();
while(payRate>75)
{
System.out.println("This employee dose not make that much");
System.out.println("Enter the employee pay rate");
//read payRate here to test payRate
payRate=in.nextDouble();
if(payRate<0)
{
System.out.println("Try again! Pay rate can not be neagitive!");
System.out.println("Enter the employee pay rate");
}
}
System.out.println("Enter the hours the employee worked: ");
double workedHour=in.nextDouble();
while(workedHour>72)
{
System.out.println("Too many hours employee worked: ");
System.out.println("Enter the hours the employee worked ");
//read workedHour from user to test workedHour
workedHour=in.nextDouble();
if (workedHour<0)
{
System.out.println("Try again! the hours emloyee worked can not be neagitive!");
System.out.println("Enter the employee hours worked");
}
}
double pay = workedHour*payRate;
System.out.println(" " +date +" "+compName +" "+first +" "+last +" "+pay);
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
sample output:
Payroll:
Enter the company's namexyz
Enter tody's date: 1/1/2015
Enter the number of employees: 2
Enter the first name and last name of the employee:
john
michelle
Enter the Employee pay rate:
100
This employee dose not make that much
Enter the employee pay rate
30
Enter the hours the employee worked:
10
1/1/2015 xyz john michelle 300.0
Enter the first name and last name of the employee:
kim
kadar
Enter the Employee pay rate:
200
This employee dose not make that much
Enter the employee pay rate
20
Enter the hours the employee worked:
50
1/1/2015 xyz kim kadar 1000.0
---------------------------------------------------------------------------------------------------------------------------------------
//Changes are in bold letters
//lottery.java
import java.util.Scanner;
public class lottery
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Scanner keyboard = new Scanner(System.in);
//set choice value to 'Y'
int choice='Y';
/*create a boolean variable repeat and set to true.
Based on this boolean value, while loop continues as long
as the repeat is true .
If user wins any one of the win case, then set repeat to false.
Then while loop stops reading input from user.*/
boolean repeat=true;
//while that repeats until repeat is false and choice is 'n'
// try-catch must be inside while loop
while (repeat&& choice=='Y')
{
try
{
int lottery= (int)(Math.random()*1000);
System.out.println("Enter your lottery pick (three digits): " );
int guess = in.nextInt();
// read new line character using nextLine
in.nextLine();
//Extracting digits from the lottery and guess numbers
int lottDigit1=lottery%10; //first digit
int tempLottery=lottery/10; //store the quotient in tempLottery number
int lottDigit2=tempLottery %10; //second digit
int lottDigit3=tempLottery /10;//third digit
int guess1= guess%10;
int tempGuess=guess/10;
int guess2= tempGuess%10;
int guess3= tempGuess / 10;
System.out.println("The lottery number is " + lottery);
if (guess == lottery)
{
System.out.println("You won $10,00");
//set repeat to false
repeat=false;
}
else if (guess3==lottDigit3&&guess2==lottDigit2&& guess1 == lottDigit1)
System.out.println("You match all digits: You won $3,000");
else if (guess1==lottDigit1
|| guess1==lottDigit2
|| guess1== lottDigit3
|| guess2 == lottDigit1
|| guess2 == lottDigit2
|| guess2 == lottDigit3
|| guess3 == lottDigit1
|| guess3 == lottDigit2
|| guess3 == lottDigit3)
{
System.out.println("Match one digit: you win 1,000");
//set repeat to false
repeat=false;
}
else
{
System.out.println("HAHA you didnt win!! Since you didn't" +
" win do you want to try again? Press Y to continue " +
"and N to end:");
//otherwise read a character and continues the loop
choice=keyboard.next().charAt(0);
}
}
catch (Exception e)
{
System.out.println("ERROR");
} //end of try -catch block
}//end of while loop
}//end of main method
}//end of the class
------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Enter your lottery pick (three digits):
111
The lottery number is 223
HAHA you didnt win!!
Since you didn't win do you want to try again? Press Y to continue and N to end:
Y
Enter your lottery pick (three digits):
242
The lottery number is 661
HAHA you didnt win!!
Since you didn't win do you want to try again? Press Y to continue and N to end:
Y
Enter your lottery pick (three digits):
253
The lottery number is 800
HAHA you didnt win!!
Since you didn't win do you want to try again? Press Y to continue and N to end:
Y
Enter your lottery pick (three digits):
222
The lottery number is 444
HAHA you didnt win!!
Since you didn't win do you want to try again? Press Y to continue and N to end:
Y
Enter your lottery pick (three digits):
452
The lottery number is 887
HAHA you didnt win!!
Since you didn't win do you want to try again? Press Y to continue and N to end:
Y
Enter your lottery pick (three digits):
929
The lottery number is 840
HAHA you didnt win!!
Since you didn't win do you want to try again? Press Y to continue and N to end:
Y
Enter your lottery pick (three digits):
241
The lottery number is 646
Match one digit: you win 1,000
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.