Java 嵌套 if 语句
Java nested if statements
我正在使用可实例化的 class 并在 Java 中嵌套 if 语句。当我 运行 这个它总是输出 500,即使输入大于 5。
夏天:
5人以下为500人,
5人以上为400
秋天:
小于 5 是 700,
大于 5 为 600
冬季:5人以内400,
5人以上为300
Spring:
小于 5 为 200,
大于 5 是 100
public class Seasons{
//data members
private String season;
private int people;
private int cost;
//constructor
public Seasons(){
season="";
people=0;
}
//set methods
public void setSeason(String season){
this.season=season;
}
public void setPeople(int people){
this.people=people;
}
//compute method
public void compute(){
if(season.equals ("summer")){
if(people<=5){
cost=500;
}
else{
cost=400;
}
}
else if(season.equalsIgnoreCase("autumn")){
if(people<=5){
cost=700;
}
else{
cost=600;
}
}
else if(season.equalsIgnoreCase("winter")){
if(people<=5){
cost=400;
}
else{
cost=300;
}
}
else if(season.equalsIgnoreCase("spring")){
if(people<=5){
cost=200;
}
else{
cost=100;
}
}
}
//get method
public int getCost(){
return cost;
}
}
应用class:
import javax.swing.JOptionPane;
public class SeasonsApp{
public static void main(String args[]){
//declare variables
String season;
int people;
int cost;
//declare objects
Seasons mySeasons;
//create objects
mySeasons=new Seasons();
//input
season=JOptionPane.showInputDialog(null,"what season is it");
people=Integer.parseInt(JOptionPane.showInputDialog(null,"How many people are going?"));
//set
mySeasons.setSeason(season);
//compute
mySeasons.compute();
//get
cost=mySeasons.getCost();
//output
JOptionPane.showMessageDialog(null,"your cost is " + cost + " euro");
}
}
你没有设置人数
添加行
mySeasons.setPeople(people);
之前
mySeasons.compute();
我正在使用可实例化的 class 并在 Java 中嵌套 if 语句。当我 运行 这个它总是输出 500,即使输入大于 5。
夏天: 5人以下为500人, 5人以上为400
秋天: 小于 5 是 700, 大于 5 为 600
冬季:5人以内400, 5人以上为300
Spring: 小于 5 为 200, 大于 5 是 100
public class Seasons{
//data members
private String season;
private int people;
private int cost;
//constructor
public Seasons(){
season="";
people=0;
}
//set methods
public void setSeason(String season){
this.season=season;
}
public void setPeople(int people){
this.people=people;
}
//compute method
public void compute(){
if(season.equals ("summer")){
if(people<=5){
cost=500;
}
else{
cost=400;
}
}
else if(season.equalsIgnoreCase("autumn")){
if(people<=5){
cost=700;
}
else{
cost=600;
}
}
else if(season.equalsIgnoreCase("winter")){
if(people<=5){
cost=400;
}
else{
cost=300;
}
}
else if(season.equalsIgnoreCase("spring")){
if(people<=5){
cost=200;
}
else{
cost=100;
}
}
}
//get method
public int getCost(){
return cost;
}
}
应用class:
import javax.swing.JOptionPane;
public class SeasonsApp{
public static void main(String args[]){
//declare variables
String season;
int people;
int cost;
//declare objects
Seasons mySeasons;
//create objects
mySeasons=new Seasons();
//input
season=JOptionPane.showInputDialog(null,"what season is it");
people=Integer.parseInt(JOptionPane.showInputDialog(null,"How many people are going?"));
//set
mySeasons.setSeason(season);
//compute
mySeasons.compute();
//get
cost=mySeasons.getCost();
//output
JOptionPane.showMessageDialog(null,"your cost is " + cost + " euro");
}
}
你没有设置人数
添加行
mySeasons.setPeople(people);
之前
mySeasons.compute();