为什么System.out.println("您需要支付"+tatPrice1+"元");中+tatPrice1+输出始终为零

import java.util.Scanner;

public class subway {

   private int price;

   private int staStation;

   private int endStation;

   private int money;

   private int number;

   public void subway(){}

   public void price1(){

  int number1=endStation-staStation;

  if(number1<=5){

  price=3;

  }

  else if(number1<=8){

  price=4;

  }

  else{

  price=5;

  }

   }

   public void shuru(){

  

  Scanner scanner =new Scanner(System.in);

  System.out.println("请输入起始站");

  staStation=scanner.nextInt();

  System.out.println("请输入终点站");

  endStation=scanner.nextInt();

  System.out.println("请输入票数");

  number=scanner.nextInt();

  

   }

   public void shuru1(){

  Scanner scanner =new Scanner(System.in);

  int tatPrice1=number*price;

  System.out.println("您需要支付"+tatPrice1+"元");

  System.out.println("请支付");

  money=scanner.nextInt();

   }

   public void pay(){

  int tatPrice=number*price;

  if(money<tatPrice){

  System.out.println("您支付的金额不足");

  }

  else if(money==tatPrice){

  System.out.println("正在帮您打印车票");

  }

  else{

  int money1=money-tatPrice;

  System.out.println("正在帮您打印车票并找您"+money1+"元");

  }

   }

public static void main(String[] args) {

subway s=new subway();

s.shuru();

s.shuru1();

s.price1();

s.pay();

}

}


不凡的蚂蚁
浏览 1180回答 1
1回答

MinRam

int tatPrice1=number*price;      // 这时的price 并未赋值(构造函数并未给price赋初值),所以默认为0 System.out.println("您需要支付"+tatPrice1+"元");   // tatPricel = number * 0 = 0
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java