类的简单Java程序

类的简单Java程序,java,Java,我想知道如何处理在我的程序中输入1个妻子、1个麻袋等的用户。为了得到生物的数量,我将它们全部相乘,但它不能解释用户输入“1”的事实,因为它只是相乘。如果用户为任何值输入“1”,则答案将不正确 import java.util.Scanner; public class Program2 { public static void main(String[] args){ Scanner keyboard = new Scanner(System.in); int wives;

我想知道如何处理在我的程序中输入1个妻子、1个麻袋等的用户。为了得到生物的数量,我将它们全部相乘,但它不能解释用户输入“1”的事实,因为它只是相乘。如果用户为任何值输入“1”,则答案将不正确

import java.util.Scanner;

public class Program2 {
public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);

    int wives;
    int sacks;
    int cats;
    int kits;

    System.out.println("St. Ives Program. Press 'Enter' to continue.");  
    try{
        System.in.read();
    } catch(Exception e){}  

    System.out.println("How many wives does the man have?");
    wives = keyboard.nextInt();

    System.out.println("How many sacks per wife?");
    sacks = keyboard.nextInt();

    System.out.println("How many cats per sack?");
    cats = keyboard.nextInt();

    System.out.println("How many kits per cat?");
    kits = keyboard.nextInt();

    System.out.println("As I was going to St. Ives,");

    //This makes it so if a user inputs "1", the correct usage of the word is displayed
    if (wives == 1){
        System.out.println("I met a man with " + wives + " wife,");
    }else{
        System.out.println("I met a man with " + wives + " wives,");
    }

    if (sacks == 1){
        System.out.println("Every wife had " + sacks + " sack,");
    }else{
        System.out.println("Every wife had " + sacks + " sacks,");
    }

    if (cats == 1){
        System.out.println("Every sack had " + cats + " cat,");
    }else{
        System.out.println("Every sack had " + cats + " cats,");
    }

    if (kits == 1){
        System.out.println("Every cat had " + kits + " kit,");
    }else{
        System.out.println("Every cat had " + kits + " kits,");
    }   

    System.out.println("Kits, cats, sacks, and wives,");
    System.out.printf("%d living things were going to St. Ives. %n", (wives * sacks * cats * kits));
    //el fin
    }
}

因为你必须加上妻子,猫,基本上

1 + wives + (wives * sacks * cats) + (wives * sacks * cats * kits)


1+
解释男人)

生活的事情=男人的数量+妻子的数量+猫的数量+装备的数量

Number of Men = 1
Number of Wives = (Number  of Wives) * (Number of Men) = (Number  of Wives)
Number of Cats = (Number  of Wives) * (Number  of Sacks for each wife) * (Number of Cats in each Sack)
Number of Kits = Number of Cats * (Number of Kits per Cat)
如果你喜欢简洁的话

   //This makes it so if a user inputs "1", the correct usage of the word is displayed
   System.out.println("I met a man with " + wives + (wives==1?" wife,":" wives,"));
您的公式错误,您只显示了套件的数量,
你需要增加猫的数量和妻子的数量


这个人也要去圣艾夫斯吗?

这一行是最后一行“System.out.printf()”,你不应该忽略异常(
catch(Exception e){}
),仅仅乘以是不够的。你需要多考虑一下你的最终配方。尝试一些不同的方法(可能还有加法),直到你得到一个可以计算任何数字(包括所有1)的公式。这不是一个Java问题,而是一个基本的算术问题。正确答案是什么?
   //This makes it so if a user inputs "1", the correct usage of the word is displayed
   System.out.println("I met a man with " + wives + (wives==1?" wife,":" wives,"));