Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法在主类中返回错误的值_Java - Fatal编程技术网

Java 方法在主类中返回错误的值

Java 方法在主类中返回错误的值,java,Java,每当我运行INSCHRIJVING应用程序时,我都会得到一个错误的值,超出了行System.out.printf(“Hoeveel Pulljes wil je bestellen?(最大%d)”,maxPulljes)导致“n”),如果答案是肯定的,则重复 Inschrijving.java package domein; public class Inschrijving { private String naam; private char categorie; private int

每当我运行INSCHRIJVING应用程序时,我都会得到一个错误的值,超出了行
System.out.printf(“Hoeveel Pulljes wil je bestellen?(最大%d)”,maxPulljes)
System.out.printf(“Tot welke categorie Behoot u?\n与沃克内姆、沃克内姆、沃克内姆和合伙人会面、沃克内姆和加斯特会面:”)

我假设int
maxfouldjes=(inschrijving.geefAantalPersonen()*5)行中有错误但似乎不知道是什么

练习是:对于邀请员工(代码中的“w”)、有合作伙伴的员工(“p”)和客人(“g”)并让他们填写姓名的公司,他们是什么样的访客(员工+合作伙伴、客人或员工),然后询问此人想要多少三明治(客人和员工最多需要5个三明治,员工+合作伙伴最多需要10个三明治),最大值以整数形式显示(最大值为%d)。所有这些都在一个循环中,直到用户在询问“Zijn er nog inschrijvingen”时写下“no”(但使用第一个字符=>导致“n”),如果答案是肯定的,则重复

Inschrijving.java

package domein;
public class Inschrijving {

private String naam;
private char categorie;
private int aantalBroodjes;

public Inschrijving(String naam, char categorie) {
    setNaam(naam);
    setCategorie(categorie);
}

public String getNaam() {
    return naam;
}

private void setNaam(String naam) {
    this.naam = naam;
}

public char getCategorie() {
    return categorie;
}

private void setCategorie(char categorie) {
    if (categorie == 'w' || categorie == 'p' || categorie == 'g') {
        this.categorie = categorie;
    } else {
        this.categorie = 'g';
    }

}

public int getAantalBroodjes() {
    return aantalBroodjes;
}

public void setAantalBroodjes(int aantalBroodjes) {

    if (aantalBroodjes <= (geefAantalPersonen() * 5)) {
        this.aantalBroodjes += aantalBroodjes;
    } else {
        this.aantalBroodjes += (geefAantalPersonen() * 2);
    }
}

public int geefAantalPersonen() {
    switch (categorie) {
        case 'w':
        case 'g':
            return 1;
        default:
            return 2;

    }
  }
}

你的方法有一些问题,它可能有效,但你不应该这样做

首先,您只在一个
Inschrijving
对象中存储为所有受邀人员请求的三明治总数,这毫无意义!(我需要知道请求的三明治总数还是只需要我请求的三明治?)。因此,将
Inschrijving
类中的
setAantalBroodjes
更改为:

public void setAantalBroodjes(int aantalBroodjes) {
  this.aantalBroodjes = aantalBroodjes;
}

第二,需求是一个列表,用它们来做一些事情,所以你应该考虑使用一个数据结构来支持你存储一个像“代码>数组”<代码>或<代码> ARARYLIST/COD>之类的人的列表,然后一旦用户停止输入,你就可以做任何你想要的数据(在你的例子中,<代码> ErrestList==n′/代码>)。


因为您是java新手,我在这里使用foreach循环来做例子,在学习OOP和熟悉java之后,我建议您重新研究java 8流api和lambda表达式来处理集合类型。

那么您得到的是什么而不是10?如果possible@JBNizet我也想到了,但这给了我一个错误应“无法到达的声明”做了一些研究给了我一个答案,因为一旦它达到了回报,它就不可能达到break@mangusta哦,是的,对不起,补充了now@user2948118对不起,忽略我之前的评论,这是错误的。我没有注意到你在那里有回复。你能给我一个解决方案吗?因为对我来说,它看起来很好。我给char categorieEersteLe的原因是值为0是因为我收到一个错误,说明它没有初始化。你想让你的
Inschrijving
类不可变吗?因为我看到你把设置器设置为私有?是的(这是大学里的练习)它清楚地说,我必须将它私有化为“p”@HaiHoang我很抱歉我以为你的意思是如果我真的想让setter私有化,我不知道什么是不可变的(final也是一样)。使用Inschrijving Inschrijving=null;Inschrijving=new Inschrijving(naam,categreeersteletter);整数确实给出了正确的值,但是aantalBroodjes现在给出了错误的值..很抱歉我是Java新手。。
public void setAantalBroodjes(int aantalBroodjes) {
  this.aantalBroodjes = aantalBroodjes;
}
List<Inschrijving> inschrijven = new ArrayList<>();
try (Scanner invoer = new Scanner(System.in)) { // http://tutorials.jenkov.com/java-exception-handling/try-with-resources.html
  System.out.println("Zijn er nog inschrijvingen? ");
  String antwoord = invoer.nextLine();
  char eersteLetter = antwoord.toLowerCase().charAt(0);
  while (eersteLetter != 'n') {

    Inschrijving inschrijving = null;
    System.out.println("Wie mag ik inschrijven? ");
    String naam = invoer.nextLine();
    char categorieEersteLetter = 0;
    do {
      System.out.printf(
          "Tot welke categorie behoort u?\nTyp w voor een werknemer, p voor een werknemer met partner, g voor een gast: ");
      String categorie = invoer.nextLine();
      categorieEersteLetter = categorie.toLowerCase().charAt(0);
    } while (categorieEersteLetter != 'w' && categorieEersteLetter != 'p' && categorieEersteLetter != 'g');
    inschrijving = new Inschrijving(naam, categorieEersteLetter);

    int maxBroodjes = (inschrijving.geefAantalPersonen() * 5);
    int tijdelijk;
    do {
      System.out.printf("Hoeveel broodjes wil je bestellen? (max %d) ", maxBroodjes);
      tijdelijk = invoer.nextInt();
      invoer.nextLine(); // https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo 
    } while (tijdelijk > maxBroodjes);
    inschrijving.setAantalBroodjes(tijdelijk);
    inschrijven.add(inschrijving);

    System.out.println("Zijn er nog inschrijvingen? ");
    antwoord = invoer.nextLine();
    eersteLetter = antwoord.toLowerCase().charAt(0);
  }
}
// Do stuffs with your list of people here
int werknemer = 0;
int werknemerMetPartner = 0;
int gast = 0;
int aantalBroodjes = 0;
for (Inschrijving inschrijving : inschrijven) {
  char categorie = inschrijving.getCategorie();
  switch (categorie) {
    case 'w':
      werknemer++;
      break;
    case 'p':
      werknemerMetPartner++;
      break;
    case 'g':
      gast++;
      break;
  }
  aantalBroodjes += inschrijving.getAantalBroodjes();
}
System.out.printf(
        "Er komen %d werknemer(s) zonder partner, %d werknemer(s) met partner en %d gast(en) naar de receptie. Er dienen %d broodjes besteld te worden.",
        werknemer, werknemerMetPartner, gast, aantalBroodjes);