New to Java, and not sure what I'm doing wrong. My If statements aren't working

huangapple go评论75阅读模式
英文:

New to Java, and not sure what I'm doing wrong. My If statements aren't working

问题

代码中的注释和字符串部分已被翻译:

// 下面的部分出现了问题。当if语句在while后面开始时,产品不会存储产品的名称(但会保留产品和稍后的成本 - 所以最终结果有效)。
// 问题是,无论输入什么,消息仍然会出现“这不是有效的产品”,即使它是有效的。这不会影响程序,但会影响输出。

import java.util.*;

public class Shopping {

  String p1Name,
  p2Name,
  p3Name; // 用于存储产品名称的变量
  double p1UP,
  p2UP,
  p3UP; // 用于存储产品单价的变量
  String c1Name,
  c2Name; // 用于存储顾客名称的变量
  double c1DRate,
  c2DRate; // 用于顾客折扣率的变量
  double quant; // 单位数量所需的变量
  double sum; // 单位和数量
  String custName; // 顾客名称
  double discount1;
  double discount2;
  String product;
  double cost = p1UP;

  Scanner scan;
  public void setProductData() {
    System.out.println("初始化产品数据");
    System.out.print("输入第1部分的名称:");
    p1Name = scan.next();
    System.out.print("输入第1部分的单价:");
    p1UP = scan.nextDouble();
    System.out.print("输入第2部分的名称:");
    p2Name = scan.next();
    System.out.print("输入第2部分的单价:");
    p2UP = scan.nextDouble();
    System.out.print("输入第3部分的名称:");
    p3Name = scan.next();
    System.out.print("输入第3部分的单价:");
    p3UP = scan.nextDouble();
  }

  public void setCustomerData() {
    System.out.println("初始化顾客数据");
    System.out.print("输入顾客1的名称:");
    c1Name = scan.next();
    System.out.print("输入顾客1的折扣率:");
    c1DRate = scan.nextDouble();

    System.out.print("输入顾客2的名称:");
    c2Name = scan.next();
    System.out.print("输入顾客2的折扣率:");
    c2DRate = scan.nextDouble();
  }
  public void computeCost() {
    // 在计算并打印成本之前,提示并读取产品名称、数量和顾客名称,考虑到顾客的特定折扣。
    boolean discount1 = false;
    boolean discount2 = false;
    System.out.print("请输入您的姓名:");
    custName = scan.next();
    if (custName.equalsIgnoreCase(c1Name)) {
      discount1 = true;
    }
    if (custName.equalsIgnoreCase(c2Name)) {
      discount2 = true;
    }
    // 确定顾客是否有资格获得折扣
    System.out.println("我们的产品和价格如下");
    System.out.println(p1Name + "的价格为" + p1UP);
    System.out.println(p2Name + "的价格为" + p2UP);
    System.out.println(p3Name + "的价格为" + p3UP);
    System.out.println("您想要什么产品");
    // 他们想要购买什么产品

    product = scan.next();

    while (!(product.equalsIgnoreCase(p1Name) || product.equalsIgnoreCase(p2Name) || product.equalsIgnoreCase(p3Name))) {
      System.out.println("这不是有效的产品。");
      product = scan.next();
    }

    if (product.equals(p1Name)) {
      cost = p1UP;
    }
    if (product.equals(p2Name)) {
      cost = p2UP;
    }
    if (product.equals(p3Name)) {
      cost = p3UP;
    }
    else {
      System.out.println("您选择了错误的产品");
      System.out.println("您已被分配" + p1Name + "而不是");
    }

    scan.nextLine();
英文:

The bottom section is where it is stuffing up. When The if statements start after while, the product doesn't store the name of the product (but keeps what product it is and the cost for later - so the end result works).
The issue is, regardless of what is entered, the message still comes up "That isn't a valid product" even if it is. It doesn't affect the program, but does affect the output.

import java.util. * ;
public class Shopping {
String p1Name,
p2Name,
p3Name; // variables for storing product names
double p1UP,
p2UP,
p3UP; // variables for storing product unit prices
String c1Name,
c2Name; // variables for storing customer names
double c1DRate,
c2DRate; // variables for customer discount rates
double quant; //variables for unit quantity required
double sum; // unit and quantity
String custName; // Customer name
double discount1;
double discount2;
String product;
double cost = p1UP;
Scanner scan;
public void setProductData() {
System.out.println("Initializing product data");
System.out.print("Enter name of part 1 : ");
p1Name = scan.next();
System.out.print("Enter unit price of part 1 : ");
p1UP = scan.nextDouble();
System.out.print("Enter name of part 2 : ");
p2Name = scan.next();
System.out.print("Enter unit price of part 2 : ");
p2UP = scan.nextDouble();
System.out.print("Enter name of part 3 : ");
p3Name = scan.next();
System.out.print("Enter unit price of part : ");
p3UP = scan.nextDouble();
}
public void setCustomerData() {
System.out.println("Initializing customer data");
System.out.print("Enter name of customer 1 : ");
c1Name = scan.next();
System.out.print("Enter discount Rate for customer 1 : ");
c1DRate = scan.nextDouble();
System.out.print("Enter name of customer 2 : ");
c2Name = scan.next();
System.out.print("Enter discount Rate for customer 2 : ");
c2DRate = scan.nextDouble();
}
public void computeCost() {
// prompt and read product name, qty and customer name before computing and
// printing the cost taking into consideration customer specific discounts.
boolean discount1 = false;
boolean discount2 = false;
System.out.print("Please enter your name : ");
custName = scan.next();
if (custName.equalsIgnoreCase(c1Name)) {
discount1 = true;
}
if (custName.equalsIgnoreCase(c2Name)) {
discount2 = true;
}
// Determined if customer eligible for a discount
System.out.println("Our products and prices are as follows");
System.out.println(p1Name + " is priced at " + p1UP);
System.out.println(p2Name + " is priced at " + p2UP);
System.out.println(p3Name + " is priced at " + p3UP);
System.out.println("What product would you like");
//what product did they want to buy
product = scan.next();
while (! (product.equalsIgnoreCase(p1Name) || product.equalsIgnoreCase(p2Name) || product.equalsIgnoreCase(p3Name))) {
System.out.println("That isn't a valid product.");
product = scan.next();
}
if (product.equals(p1Name)) {
cost = p1UP;
}
if (product.equals(p2Name)) {
cost = p2UP;
}
if (product.equals(p3Name)) {
cost = p3UP;
}
else {
System.out.println("You have selected an incorrect product");
System.out.println("You have been assigned " + p1Name + " instead");
}
scan.nextLine();

答案1

得分: 1

我觉得你这里出了一个错误。

我猜你是想在输入不匹配p1Namep2Namep3Name中的任何一个时打印消息"这不是一个有效的产品",对吗?

所以你的while条件必须是不等于p1Name且不等于p2Namep3Name

你可以使用这个:

while (!(product.equalsIgnoreCase(p1Name) || product.equalsIgnoreCase(p2Name) || 
        product.equalsIgnoreCase(p3Name))) {
    System.out.println("That isn't a valid product.");
    product = scan.next();
}
英文:

I think you're having a mistake here.

I suppose you're trying to print the message "That isn't a valid product." if the input does not match any of p1Name, p2Name and p3Name, right?

So your while condition must be NOT EQUALS to p1Name AND p2Name, p3Name too.

So you can use this:

while (!(product.equalsIgnoreCase(p1Name) && !(product.equalsIgnoreCase(p2Name)) && ! 
(product.equalsIgnoreCase(p3Name)))) {
System.out.println("That isn't a valid product.");
product = scan.next();
}

答案2

得分: 0

在您的代码中,建议在扫描时使用String.trim(),以便同时扫描空格或空白字符。

在您的while条件中:

!(product.trim().equalsIgnoreCase(p1Name.trim()) || product.trim().equalsIgnoreCase(p2Name.trim()) || product.trim().equalsIgnoreCase(p3Name.trim()))
英文:

use String.trim() maybe when you scan you scan also the space or whitespace character.

in ur while condition :

!(product.trim().equalsIgnoreCase(p1Name.trim()) || product.trim().equalsIgnoreCase(p2Name.trim()) || product.trim().equalsIgnoreCase(p3Name.trim()))

答案3

得分: 0

好的,我搞清楚了。
我之前使用了 if,
而不是后面的 else if 语句。
我的错。

谢谢。

英文:

Ok, I worked it out.
I had if
not else if for the later statements.
my bad.

Thanks

huangapple
  • 本文由 发表于 2020年8月3日 11:06:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63223180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定