如何将用户输入多次,直到数组中的所有匹配字符都替换为0 [英] How do I ask a user for input several times until all matching characters in an array are replaced by 0

查看:69
本文介绍了如何将用户输入多次,直到数组中的所有匹配字符都替换为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序,创建一个随机填充(AZ)的5个字符的数组,然后要求用户使用这个输入一个字符

1 java.util.Scanner in = new java .util.Scanner(System.in);

2

3 //。 。 。 。

4

5 char c = in.next()。charAt(0);



then程序必须计算给定数组中的出现次数,在控制台中打印出计数并将给定数组中的匹配字符替换为0。当整个数组填满'0'时,程序必须完成。



这是我到目前为止所拥有的:

I need to write a program which creates an array of 5 characters randomly filled(A-Z), then asks the user to input a character using this
1 java.util.Scanner in = new java.util.Scanner(System.in);
2
3 // . . . .
4
5 char c = in.next().charAt(0);

then the program has to count occurrances in the given array, print out the count in console and replace matching characters in the given array by '0'. The program has to finish when whole array is filled with '0'.

Here's what I have so far:

public class New{
    public static void main(String[] args){

        char arr[] = new char[5];
        for(int i=0;i<arr.length;i++){
            arr[i] = (char)(Math.random()*((90-65)+1)+65);
        }
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+ " ");
        }

        java.util.Scanner in = new java.util.Scanner(System.in);
        int counter = 0;
    
        char c = in.next().charAt(0);
       

        for(int j = 0;j<arr.length;j++){
            if(arr[j] == c){
                counter++;
                arr[j]= '0';
            }
        }
        System.out.println("The character is present " + counter+" times");

        for(int j = 0;j<arr.length;j++){
            System.out.print(arr[j]+ " ");
        }





上面的代码请求输入的代码,替换字符并打印字符所在的次数数组。问题是我需要多次询问用户输入,直到整个数组都填满零。



我尝试过:



我试图放置char c = in.next()。charAt(0);进入另一个循环(外部循环),我在上面的代码中有一个循环内部,它没有工作



我试图在Google上找到答案但我找不到任何适合这种情况的东西。



我不能使用课堂上没有讨论的方法,所以必须有一种方法来放置char c = in.next()。charAt(0 );进入一个循环,以便多次要求用户输入。



The code above asks for input ones, replaces the character and prints out the number of times that character was in the array. The problem is that I need to ask the user for input several times until whole array is filled with zeros.

What I have tried:

I tried to place char c = in.next().charAt(0); into another loop(external loop) and the one I have in the code above was inside that loop, it didn't work

I tried to find an answer on Google but I couldn't find anything that would fit in this situation.

I can't use methods which were not discussed in class, so there has to be a way to place char c = in.next().charAt(0); into a loop so that a user is asked for input several times.

推荐答案

你有没有考虑过

Have you considered
java.util.Scanner in = new java.util.Scanner(System.in);
do {
   char c = in.next().charAt(0);
   ... // do something with the input.
   } while (thereIsSomethingLeftInTheArray);



while和do-while语句(The Java™Tutorials:Language Basics) [ ^ ]


你可以使用 do-while loop:

You might use a do-while loop:
public class New
{
  public static void main(String[] args)
  {
    char arr[] = new char[5];
    for(int i=0;i<arr.length;i++)
      arr[i] = (char)(Math.random()*((90-65)+1)+65);

    for(int i=0;i<arr.length;i++)
      System.out.print(arr[i]+ " ");

    java.util.Scanner in = new java.util.Scanner(System.in);
    int zeroes;
    do
    {
      int matches = 0;
      zeroes = 0;
      System.out.println("Please enter a character");
      char c = in.next().charAt(0);

      for (int j = 0; j<arr.length; ++j)
      {
        if (arr[j] == c)
        {
          ++matches;
          arr[j]= '0';
        }
        if ( arr[j] == '0')
          ++zeroes;
      }
      System.out.println("The character is present " + matches + " times");
    } while (zeroes < arr.length);
    System.out.println("Well done");
  }
}


这篇关于如何将用户输入多次,直到数组中的所有匹配字符都替换为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆