简体   繁体   中英

Using a for loop to call a character method multiple times. The for loop will then convert each character to a string

The output should look like the attached screenshot. I am stuck on the very last step. Using a for loop to call the getCharacter method 10 times and converting the characters to a string using the Character.toString() method. 在此处输入图片说明

`` public static void main(String[] args) {

   int count = countNumbers();
  
   countPlay(count);
   
   String word = getCharacter();
   
   stringOf10(word);
   
   
}
public static double getRealNumber(){
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a real number, one that has a decimal point: ");
    double realNumber = sc.nextDouble();
    return realNumber;
}
public static int countNumbers(){
    int count = 0;
    do{
      if (getRealNumber() == -1.0)
            break;
      count++;
      } while(true);   
    System.out.println("The count is " + count);
    return count;
}
public static int countPlay(int count){
    int exponent = 4;
    double result = 0;
    result = Math.pow(count, exponent);
    System.out.println(count + "^" + exponent + " is " + result );
    return count;
}
public static char getCharacter(){
    Scanner input = new Scanner(System.in);
    System.out.println("You will be asked to enter 10 characters.");
    System.out.print("Enter a character: ");
    char character = input.next().charAt(0);
    return character;
}
public static char stringOf10(String word){
    char i = getCharacter();
    i = Character.toString(word) ;
    for (i = 0; i < 10; i++){
    }
    return word;
}

}

``

Consider the following block of code which will run in the main method:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    String word = "";
    //Print the instructions before the loop
    System.out.println("You will be asked to enter 10 characters.");
    //request characters inside the loop
    for (int i = 0; i < 10; i++){
        System.out.print("Enter a character: ");
        word += input.next().charAt(0);
    }
    //print the result (or return from a method)
    System.out.println("The word is: " + word);
    //return word;
}

Besides the corrections in comments, note how we create a local variable word , and simply update/append that inside the for loop word += input.next().charAt(0); .

You can easily move this code to fit your needs and return the value to be printed:

public static void main(String[] args) {

    //Call the method to get our word and save the result
    String result = stringOf10();
    //Print the result
    System.out.println("The word is: " + result);
}

public static String stringOf10(){
    //Creater a scanner once before the loop
    Scanner input = new Scanner(System.in);
    //Create a local variable to store the word as it is updated
    String word = "";
    //Print the instructions once before the loop
    System.out.println("You will be asked to enter 10 characters.");

    //Create a loop that will run 10 times "for (int i = 0; i < 10; i++)"
    //The loop starts by creating an int that is 0 "int i=0"
    //Each time the loop ends it will do "i = i+1"
    //The loop will run until i is no longer less than 10 "i < 10"
    //Once that happens the loop will end and the code after the loop will run
    for (int i = 0; i < 10; i++){
        //Dach time the loop will call this method and update the word
        word += getCharacter(input);
    }
    return word;
}

public static char getCharacter(Scanner input){
    //Each time this method is called we prompt the user to enter a character
    System.out.print("Enter a character: ");
    //We then return the character to the previous method
    return input.next().charAt(0);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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