2016-04-08 53 views
1

我是java新手,上週已經自學了。我無法找到if else語句運行兩次的原因。這裏是整個代碼,我知道很簡單,但仍然在努力學習。爲什麼我的掃描儀(system.in)運行兩次

package tickets; 

import java.util.Scanner; 


public class tickets { 
public static void main(String[] args) { 
//program designed to ask how many visitors 
//are in a party of people and work out 
//the total cost of the entry tickets. 
    double adult = 12.50; 
    double consession = 9.90; 
    double child = 6.25; 
    double percentage = 0.80; 

System.out.println("please enter the amount of adults"); 
Scanner adult1 = new Scanner (System.in); 

//adding code that would give a percentage discount for 
//4 adults or more 
{ 
if (adult1.nextInt() >= 4 
{ 
double adult2 =(adult1.nextInt() * percentage); 
      }else { 
double adult2 = (adult * adult1.nextInt()); 



System.out.println("please enter the amount of consessions");  
    Scanner consession1 = new Scanner (System.in); 
      double consession2 = (consession *consession1.nextInt()); 

    System.out.println("please enter the amount of children"); 
    Scanner child1 = new Scanner (System.in); 
      double child2 = (child * child1.nextInt()); 

    System.out.println("total"+" " + (adult2 +consession2 + child2)); 
    System.out.println("hope you enjoy your visit today!"); 
    //woop woop it works!!!!!!!!!! 
     } 

    } 
} 

}

+0

請張貼其餘的代碼,並請修復代碼格式的縮進。 –

+0

另外,你可以定義「運行兩次」?上面例子中的代碼行將被執行兩次。你看到了什麼行爲或錯誤? –

+4

我猜這個問題不是'if'運行兩次,而是你在代碼中調用'nextInt'兩次。 – fabian

回答

2

商店int從掃描儀和使用您的if S和計算該值。您不止一次地致電nextInt(),並且您每次獲得另一個int

輸入if後,您將等待停止程序的整數類型的更多輸入。

相關問題