2017-07-29 59 views
-4

我怎樣才能從掃描儀使用一個變量加起來3個輸入,而僅用於循環(無數組)的&? CLICK THIS LINK TO SEE IMAGE INCLUDING INSTRUCTIONSwhile和for loops

這是該代碼是完成圖像任務的必要條件。

import java.util.Scanner; 
public class Ass1b 
{ 
    public static void main (String[]args) 
    { 
    String taxPayerName; 
    int totalInc; 
    double totalTax; 
    Scanner inText = new Scanner(System.in); 
    System.out.print("Please enter the name of the tax payer==> "); 
    taxPayerName = inText.nextLine(); 


    Scanner inNumber = new Scanner(System.in); 
    System.out.print("Enter the income for "+ taxPayerName +" ==> "); 
    totalInc = inNumber.nextInt(); 
    if (totalInc < 18200) 
    { 
     totalTax = 0; 
     System.out.print("The tax that " + taxPayerName + " has to pay is $"+ totalTax); 
    } 
    else if(totalInc < 37000) 
    { 
     totalTax=((totalInc - 18200)* 0.19); 
     System.out.print("The tax that " + taxPayerName + " has to pay is $"+ totalTax); 
    } 
    else if(totalInc < 87000) 
    { 
     totalTax=(3572 +(totalInc - 37000)* 0.325); 
     System.out.print("The tax that " + taxPayerName + " has to pay is $"+ totalTax); 
    } 
    else if(totalInc < 180000) 
    { 
     totalTax=(19822 +(totalInc - 87000)* 0.37); 
     System.out.print("The tax that " + taxPayerName + " has to pay is $"+ totalTax); 
    } 
    else 
    { 
     totalTax = (54232 + (totalInc - 180000)*0.47); 
     System.out.print("The tax that " + taxPayerName + " has to pay is $"+ totalTax); 
    } 

    } 
} 

回答

0

因此,主要問題似乎是:如何從掃描儀加起來3個輸入?

一個掃描儀可以多次使用,例如:

public static void main(String[] args){ 
    Scanner scanner = new Scanner(System.in); 

    for (int i = 0; i < 5; i++){ 
     System.out.println("value : " + scanner.nextInt()); 
    } 
} 

此代碼將for循環和一個掃描器對象詢問用戶五次一個整數。有了這個,你應該可以完成練習。現在你只需要有一個追蹤總稅額的變量,然後計算平均稅。

在未來嘗試提出一個更具體的問題,而不是隻是要求答案,因爲看起來這是一項家庭作業。所以我試圖用一般的代碼來幫助你,它不直接回答問題。