老師要求的用戶界面顯示結果如下:
This program computes the total cost of a purchase. Enter the price of the purchased item. 12.50
Enter how many items are purchased? 3
The final cost of the purchase with the 7% tax applied is $40.125
以下是我的代碼:
package jave.util;
import java.util.Scanner;
public class Assignment1 {
public static void main(String[] args) {
System.out.println("Please input the number");
Scanner N= new Scanner(System.in);
System.out.println("Please input the price");
Scanner P= new Scanner(System.in);
int number=N.nextInt();
double price=P.nextDouble();
double cost= (price*number*1.07);
System.out.println("The final cost of the purchase with the 7% tax applied is " + cost);
// TODO Auto-generated method stub
}
}
此時我輸入數值的時候,兩個數值是連續輸入的,請問怎麼編寫才能令輸入一個數值之後,再提示輸入另外一個?(另外代碼有什麼可以優化的地方也請知道一下)
調整後大致如下,不需要兩個Scanner 對象,也不要太多的變量聲明
package jave.util;
import java.util.Scanner;
public class Assignment1 {
public static void main(String[] args) {
Scanner N= new Scanner(System.in);
System.out.println("Please input the number");
int number=N.nextInt();
System.out.println("Please input the price");
double price=N.nextDouble();
System.out.println("The final cost of the purchase with the 7% tax applied is $" +(price*number*1.07));
}
}