简体   繁体   English

关于带字符数组的 for 循环的基本问题(Java)

[英]Basic question about for loop with char array (Java)

My assignment is to go through a users input and convert it to a total sum.我的任务是通过用户输入 go 并将其转换为总和。 Idea is to change all the letters to a corresponding number, as in a = 1 and so on.想法是将所有字母更改为相应的数字,如a = 1等等。

Really basic stuff but I'm at a loss, my idea was to convert the users response to a char array and then loop through each char and then use a switch or multiple loops to get the value but I can't even get a for loop to work because I'm getting "Cannot invoke charAt(int) on the array type char[]" .非常基本的东西,但我不知所措,我的想法是将用户响应转换为 char 数组,然后循环遍历每个 char,然后使用开关或多个循环来获取值,但我什至无法获得 for循环工作,因为我得到"Cannot invoke charAt(int) on the array type char[]"

public class question3 {

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

        String wordValue;
        System.out.print("Please enter a string");
        wordValue = userTypes.next();

        String lowerCase;
        lowerCase = wordValue.toLowerCase();

        char[] arrayConvert = lowerCase.toCharArray();

        System.out.println(arrayConvert);
        int fullNumber;
        System.out.print("Please enter an int");
        fullNumber = userTypes.nextInt();

        double decimalNumber;
        System.out.print("Please enter a double");
        decimalNumber = userTypes.nextDouble();

        double totalNumber;
        totalNumber = fullNumber + decimalNumber;
        System.out.print("your result is " + totalNumber);

        for(int i=0; i< arrayConvert.length;i++) {
            if(arrayConvert.charAt(i)== ("a")){

            }
        }
    }

I didn't test your code, but charAt isn't a char[] method.我没有测试您的代码,但charAt不是 char[] 方法。 Try this:尝试这个:

for(int i=0; i< arrayConvert.length;i++) {
    if(arrayConvert[i] == 'a'){

    }
}

My first question, Why do you want to get a string and converting into int or long .我的第一个问题,为什么要获取string并转换为intlong It looks you need to do code for all the inputs (i,e- if you want to get 1000 values as inputs then you need to write code stdout and next* operation for 1000 times.看起来您需要为所有输入编写代码(即,如果您想获得 1000 个值作为输入,那么您需要编写代码 stdout 和 next* 操作 1000 次。

Try the below one which will help users to choose their choice of inputs and get the sum value of those inputs.试试下面的方法,这将帮助用户选择他们的输入选择并获得这些输入的总和值。

SumGivenInputs.java SumGivenInputs.java

import java.util.Arrays;
import java.util.Scanner;

public class SumGivenInputs {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter the number of inputs\n");
       int noOfInput = scan.nextInt();

       System.out.printf("Enter the %d input%s \n", noOfInput, noOfInput > 1 ? "s one by one" : "");
       Integer[] inputList = new Integer[noOfInput];
       for (int i = 0; i < noOfInput; i++) {
           inputList[i] = scan.nextInt();
       }
       int result = 0;
       for (Integer input : Arrays.asList(inputList)) {
           result += input;
       }
       System.out.printf("Sum = %s", result);
   }
}

Output: Output:

Enter the number of inputs
5
Enter the 5 inputs one by one 
10
20
30
5
50
Sum = 115

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM