2016-04-08 16 views
1

私は先週自己教えていたjavaの新人です。 if else文が2回実行される理由を見つけることができません。ここでは全体のコードですが、私は知っているシンプルですが、まだ勉強しようとしています。私のスキャナ(system.in)が2回実行されるのはなぜですか?

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

また、「2回実行」を定義できますか?上の例のコード行は2回実行されません。どのような動作やエラーが表示されますか? –

+4

問題は 'if'が2回実行されているのではなく、コード内で' nextInt'を2回呼び出すことであると思います。 – fabian

答えて

2

ストアintスキャナから、あなたif秒と計算して、その値を使用します。あなたはnextInt()を2度以上呼び出すたびに、もう1度はintになるでしょう。

ifまたはelseを入力すると、整数型の入力を待ってプログラムを停止します。

関連する問題