2014-01-29 43 views
0

我需要爲我的CIS課程編寫一個程序,我覺得我已經成立了多數人。 95%,總結簡單。我必須編寫一個程序,提示用戶輸入他們的姓名,工資,工作時間。然後它獲取數據,計算總工資,然後減去稅收,然後將其全部打印到屏幕上。該計劃應該允許多個用戶爲員工的數量添加數據,這迫使我相信我需要設置他們必須輸入的值來結束程序。問題是我很遺憾地難以編碼,所以當輸入值時,程序結束。我幾乎肯定需要一段時間循環,但任何形式的反饋都將非常感激。提示程序結束

package program1; 

import java.util.*; 


public class Program1 { 

static Scanner console = new Scanner (System.in); 

public static void main(String[] args) { 

    String firstName, lastName ; 
    double payRate; 
    int hoursWorked; 
    double netPay; 
    double grossPay; 
    String formatNet; 


    System.out.println("Please enter the employee's name. (Enter a -1 when finished): ")   ; 
    firstName = console.nextLine(); 
    lastName = console.nextLine(); 

    System.out.println("Please enter the employee's pay rate. "); 
    payRate = console.nextDouble(); 

    System.out.println("Please enter the employee's hours worked. "); 
    hoursWorked = console.nextInt(); 

    if(hoursWorked > 40) 
    { 
     grossPay = payRate * hoursWorked * 1.5; 
    } 
    else 
    { 
     grossPay = payRate * hoursWorked; 
    } 

    netPay = grossPay - (grossPay * .15); 
    formatNet = String.format("%.2f", netPay); 
    System.out.println(firstName +" "+ lastName + "'s net pay is " + formatNet); 

} 

} 
+2

這個';'的縮進非常難看。 – Maroun

+0

現在是?在網站上它可能看起來很糟糕。但是在像NetBeans這樣的程序中,它似乎是有組織的,對我來說更加乾淨。我認爲我從我以前的一位教授那裏得到一個習慣,他告訴我有一個乾淨的有組織的代碼總是一個好習慣。你的想法? – Leoinu

+0

如果我想縮進行,我縮進變量,而不是';'。 – Maroun

回答

0

是的,你需要一個循環。試試下面的代碼。

還請注意在
console.nextDouble();console.nextInt();之後存在console.nextLine();

public static void main(String[] args) { 

    String firstName, lastName; 
    double payRate; 
    int hoursWorked; 
    double netPay; 
    double grossPay; 
    String formatNet; 

    while (true){ 
     System.out.println("Please enter the employee's name. (Enter a -1 when finished): "); 
     firstName = console.nextLine(); 
     if ("-1".equals(firstName.trim())) break; 
     lastName = console.nextLine(); 

     System.out.println("Please enter the employee's pay rate. "); 
     payRate = console.nextDouble(); 
     console.nextLine(); 

     System.out.println("Please enter the employee's hours worked. "); 
     hoursWorked = console.nextInt(); 
     console.nextLine(); 

     if (hoursWorked > 40) { 
      grossPay = payRate * hoursWorked * 1.5; 
     } else { 
      grossPay = payRate * hoursWorked; 
     } 

     netPay = grossPay - (grossPay * .15); 
     formatNet = String.format("%.2f", netPay); 
     System.out.println(firstName + " " + lastName + "'s net pay is " + formatNet); 
    } 

} 
+0

我不願意要求更多的舉手,但恐怕我不明白你最近的編輯會得到什麼。 – Leoinu

+0

讓我重述一下我以前的問題。爲什麼必須有一個console.nextLine();在他們之後? – Leoinu

+0

nextDouble/nextInt不會消耗行尾的\ n。試試這兩個版本,你會發現,我想。 –

0

使用do while循環,通過使用一些條件,你可以終止循環一樣可以提示用戶爲「你要繼續(Y/N)」,並根據價值,你可以再次重複。

編輯

do 
    { 
     //your code goes here 
     System.out.println("Do you want to continue(y/n)?"); 
     isContinue = console.next(); 
    }while(isContinue = "Y" || isContinue = "y") 

您可以在if條件

+0

我想盡可能多!在while()期間我需要使用NOT運算符嗎? – Leoinu

+0

嘗試編輯的代碼。 – eatSleepCode

+0

它不能提示用戶是否要繼續。學生必須非常具體,這就是爲什麼當它提示輸入名稱時說:「完成後輸入-1」,否則我會首先完成這個任務。 – Leoinu

0

這裏檢查firstname is -1後使用break語句是一個解決方案。我投入了一些面向對象,方法提取和資源管理,以便在使用後始終關閉掃描儀。

import java.util.Scanner; 

public class Program1 { 

    public static void main(String[] args) { 
     Scanner console = new Scanner(System.in); 
     try { 
      do { 
       enterEmployee(console); 
       System.out.println("Another employee? (q to quit)"); 
      } while (!"q".equals(console.nextLine())); 
     } finally { 
      console.close(); 
     } 
    } 

    private static void enterEmployee(Scanner console) { 
     Employee employee = new Employee(); 
     System.out.println("Please enter the employee's first name: "); 
     employee.setFirstName(console.nextLine()); 

     System.out.println("Please enter the employee's last name: "); 
     employee.setLastName(console.nextLine()); 

     System.out.println("Please enter the employee's pay rate: "); 
     employee.setPayRate(console.nextDouble()); 
     console.nextLine(); 

     System.out.println("Please enter the employee's hours worked: "); 
     employee.setHoursWorked(console.nextInt()); 
     console.nextLine(); 

     System.out.println(employee + "'s net pay is " + String.format("%.2f", employee.getNetPay())); 
    } 

    public static class Employee { 
     private String firstName; 
     private String lastName; 
     private double payRate; 
     private int hoursWorked; 
     private double netPay; 
     private double grossPay; 

     public String getFirstName() { 
      return firstName; 
     } 

     public void setFirstName(String firstName) { 
      this.firstName = firstName; 
     } 

     public String getLastName() { 
      return lastName; 
     } 

     public void setLastName(String lastName) { 
      this.lastName = lastName; 
     } 

     public double getPayRate() { 
      return payRate; 
     } 

     public void setPayRate(double payRate) { 
      this.payRate = payRate; 
     } 

     public int getHoursWorked() { 
      return hoursWorked; 
     } 

     public void setHoursWorked(int hoursWorked) { 
      this.hoursWorked = hoursWorked; 
      if (hoursWorked > 40) { 
       grossPay = payRate * hoursWorked * 1.5; 
      } else { 
       grossPay = payRate * hoursWorked; 
      } 

      netPay = grossPay - (grossPay * .15); 
     } 

     public double getNetPay() { 
      return netPay; 
     } 

     public double getGrossPay() { 
      return grossPay; 
     } 

     @Override 
     public String toString() { 
      return firstName + " " + lastName; 
     } 
    } 
}