gpt4 book ai didi

java - 如何让用户选择输入Java

转载 作者:行者123 更新时间:2023-12-01 11:27:15 25 4
gpt4 key购买 nike

有人可以解释一下如果用户选择 2 的选项如何执行 if 语句吗?

import java.util.Scanner;

public class PostalTest
{

public static void main(String args []){

Scanner type = new Scanner(System.in);

String object;
Double weighte, weightl;

System.out.println("\nWhat type of package do you have?\n");
System.out.println("\nMin Weight = 1oz. -- Max weight = 3.5oz for Letters 13 oz for Envelopes\n");
System.out.println("(1) - Envelope");
System.out.println("(2) - Letter");

System.out.print("Please enter your selection:\t");
object = type.nextLine();


if (object.equals("1"))
System.out.print("Enter a weight:\t");
weighte = type.nextDouble();

if (weighte > 13 )
System.out.println("Too Big of a Package!");

else if (weighte == 13)
System.out.println("price = $3.62");

else if (weighte >= 12 )
System.out.println("price = $3.40");

else if (weighte >= 11 )
System.out.println("price = $3.18");

else if (weighte >= 10 )
System.out.println("price = $2.96");

else if (weighte >= 9 )
System.out.println("price = $2.74");

else if (weighte >= 8 )
System.out.println("price = $2.52");

else if (weighte >= 7 )
System.out.println("price = $2.30");

else if (weighte >= 6 )
System.out.println("price = $2.08");

else if (weighte >= 5 )
System.out.println("price = $1.86");

else if (weighte >= 4 )
System.out.println("price = $1.64");

else if (weighte >= 3 )
System.out.println("price = $1.42");

else if (weighte >= 2 )
System.out.println("price = $1.20");

else if (weighte >= 1 )
System.out.println("price = $.98");

else if (weighte < 1 )
System.out.println("Package is too small!");

else
System.out.println("Not a valid Package!");

else if (object.equals("2"))
System.out.print("Enter a weight:\t");
weightl = type.nextDouble();

if (weightl > 3.5 )
System.out.println("Too Big of a Package!");

else if (weightl == 3.5)
System.out.println("price = $1.15");

else if (weightl >= 3 )
System.out.println("price = $.93");

else if (weightl >= 2 )
System.out.println("price = $.71");

else if (weightl >= 1 )
System.out.println("price = $.71");

else if (weightl < 1 )
System.out.println("Package is too small!");


}
}

最佳答案

父项缺少大括号 if else, if (object.equals("1")) 没有任何左大括号等,现在试试这个。我已经为您突出显示了代码更改。

public class PostalTest{
public static void main(String args[]) {

Scanner type = new Scanner(System.in);

String object;
Double weighte, weightl;

System.out.println("\nWhat type of package do you have?\n");
System.out.println("\nMin Weight = 1oz. -- Max weight = 3.5oz for Letters 13 oz for Envelopes\n");
System.out.println("(1) - Envelope");
System.out.println("(2) - Letter");

System.out.print("Please enter your selection:\t");
object = type.nextLine();

if (object.equals("1")) {

            System.out.print("Enter a weight:\t");
weighte = type.nextDouble();

if (weighte > 13)
System.out.println("Too Big of a Package!");

else if (weighte == 13)
System.out.println("price = $3.62");

else if (weighte >= 12)
System.out.println("price = $3.40");

else if (weighte >= 11)
System.out.println("price = $3.18");

else if (weighte >= 10)
System.out.println("price = $2.96");

else if (weighte >= 9)
System.out.println("price = $2.74");

else if (weighte >= 8)
System.out.println("price = $2.52");

else if (weighte >= 7)
System.out.println("price = $2.30");

else if (weighte >= 6)
System.out.println("price = $2.08");

else if (weighte >= 5)
System.out.println("price = $1.86");

else if (weighte >= 4)
System.out.println("price = $1.64");

else if (weighte >= 3)
System.out.println("price = $1.42");

else if (weighte >= 2)
System.out.println("price = $1.20");

else if (weighte >= 1)
System.out.println("price = $.98");

else if (weighte < 1)
System.out.println("Package is too small!");

else
System.out.println("Not a valid Package!");

} else if (object.equals("2")) {

            System.out.print("Enter a weight:\t");
weightl = type.nextDouble();

if (weightl > 3.5)
System.out.println("Too Big of a Package!");

else if (weightl == 3.5)
System.out.println("price = $1.15");

else if (weightl >= 3)
System.out.println("price = $.93");

else if (weightl >= 2)
System.out.println("price = $.71");

else if (weightl >= 1)
System.out.println("price = $.71");

else if (weightl < 1)
System.out.println("Package is too small!");
}

}

}

关于java - 如何让用户选择输入Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com