有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

找不到java Bash命令,并且方法有问题

我对F.A、B、C、D方法的所有工作都有困难。对于方法F,我一直没有找到bash命令

方法A打印3个双输入中的最小数量

方法B平均三个整数输入

方法C取整数中的第一个数字

方法D取整数中的最后一位

方法E计算整数中的位数

方法F应该查找字符串中的字母数量,因此,“!!!fd!!!”将只打印出2(目前不完整)

我主要关心的是:当我运行代码时,它要求输入一个字符串,并且用户在代码中键入,我会不断得到bash:whatTheUserTypes:com,但找不到。我不知道哪里不对,也不知道如何实现方法F的目标

导入java。util。扫描仪

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

//uses part A, prints smallest number
    System.out.println("Give me 3 doubles with a space in between each.");

        double a = input.nextDouble();
        double b = input.nextDouble();
        double c = input.nextDouble();

    double buc = smallest(a, b, c);
        System.out.println(buc + " is the smallest of the 3.");
                System.out.println("");


//uses part B, takes avg of 3 ints
    System.out.println("Give me 3 integers with a space in between each.");

        int e = input.nextInt();
        int v = input.nextInt();
        int g = input.nextInt();

    int avg = average(e, v, g);
        System.out.println(avg + " is the average of the 3 numbers.");
                System.out.println("");


//uses part C, D, and E, prints first digit and below the last digit, and how many digits there are.
    System.out.println("Give me an integer.");

        int n = input.nextInt();

    int fstdig = firstDigit(n);
    int lstdig = lastDigit(n);
    int numdig = digits(n);
        System.out.println(fstdig + " is the first digit in the number.");
        System.out.println(lstdig + " is the last digit in the number.");
        System.out.println("There are " + numdig + " digits in this number.");
                System.out.println("");

//usses f
    System.out.println("Give me a string.");

    String word = input.nextLine();
        System.out.println("litarly a test" word);


    

  }
  
  //A
  public static double smallest (double a, double b, double c)
  {
    if (a < b && a < c){
        return a;

    }
      
    else if (b < a && b < c){
        return b;
    }     
    else
        return c; 
  }
  
  
  //B
  public static int average (int e, int v, int g)
  {
    return((e * v * g) / 3);
  }
  
  
  
  //C
    public static int firstDigit  (int n)
  {
     while (n > 9) {
        n /= 10;
    }
    return n;
  }


  //D
  public static int lastDigit  (int n)
  {  
    return Math.abs(n) % 10;
  }


  //E
  public static int digits (int n)
  {  
      String numberString = Integer.toString(n);
      int numbers = numberString.length();
        return numbers;
  }


  //F
  public static String countLetters(String word) 
  {
        return word;
  }
  
  
  //G
  
  
}

共 (1) 个答案

  1. # 1 楼答案

    您的问题似乎是您的程序在您预期的时间之前退出。原因是,在使用input.nextInt进行整数输入,然后使用input.nextLine进行字符串输入之后,必须进行两次调用,将第一个结果丢弃。原因是,您必须读取用户输入的最后一组数字后面的下线字符。然后,您可以读取下一行输入,该行处理用户输入的文本及其后面的EOL

    我不知道你在说什么,但是我猜你没有意识到你的程序已经退出了,所以你正在输入一些你认为程序正在读取的东西,相反,Bash shell正在尝试处理你键入的内容

    所以说清楚一点,试着用这个作为你的“F”部分:

    //usses f
    input.nextLine(); // burn the EOL from the last line of integers input
    System.out.println("Give me a string.");
    String word = input.nextLine();
    System.out.println("litarly a test: " + word);
    

    注意:上面的最后一行有语法错误。你错过了+