Java 查找用户连续输入数字的次数。使用循环

Java 查找用户连续输入数字的次数。使用循环,java,loops,Java,Loops,我承认,这确实是一个学校项目。这是问题本身,因为我觉得它太复杂了,无法解释: Write a JAVA program that prompts the user to enter a positive integer and store it in a variable named n. You may assume without error checking that the value n is positive. The user is then prompted to ente

我承认,这确实是一个学校项目。这是问题本身,因为我觉得它太复杂了,无法解释:

Write a JAVA program that prompts the user to enter a positive integer and store it in a 
variable named n.
You may assume without error checking that the value n is positive.  
The user is then prompted to enter n number of integers.  After the n integers have been 
entered, the program then prompts the user to enter another single integer to be stored in
a variable named key. The program must now determine and output the maximum number of 
times the value key was entered consecutively. (If the key was never entered, output 0. If
it was entered, but never twice in a row, output 1.) 
关于文字墙的事,我很抱歉,但你有它。现在,我不是来乞求答案的。我有一些代码,我一直在努力工作,但我不能找出连续的部分。这是:

import java.util.Scanner;
public class Consecutive {
    public static void main(String[] args) {

  Scanner kbd = new Scanner(System.in);
  System.out.print("Enter a positive integer: ");
     String n = kbd.nextLine();

  System.out.print("Now, enter "+n+" integer(s), of any kind: ");    
     int any = kbd.nextInt();

  do {                                    
    System.out.println("Now enter one integer, the key: ");
      int key = kbd.nextInt();
    for (int i = 0; i < n.length(); i++) {
                } // Havent figured out this part yet, or if its even needed.
      } while (any == n.length());



}
}
import java.util.Scanner;
公开课{
公共静态void main(字符串[]args){
扫描仪kbd=新扫描仪(System.in);
System.out.print(“输入正整数:”);
字符串n=kbd.nextLine();
System.out.print(“现在,输入“+n+”整数,任何类型:”);
int any=kbd.nextInt();
做{
System.out.println(“现在输入一个整数,键:”);
int key=kbd.nextInt();
对于(int i=0;i

我知道这不是最好的东西。任何帮助都将不胜感激。您有一个愉快的夜晚。

再详细讨论一下规范,您已经在用户输入密钥的过程中进行了循环,但是在输入所有数字后,密钥应该只输入一次,对吗?重新思考实际需要重复的过程,然后再次询问是否卡住:)

System.out.print(“现在,输入“+n+”整数,任何类型:”
int any=kbd.nextInt()

在这句话中,您不接受n个数字。您只接受用户的一个号码。数组是一种将相同数据类型的多个值存储在一起的方法。您可能需要查找和阅读java中的数组

此外,您正在do while循环中接受来自用户的密钥。您只需要输入一次键。因此,您可能希望将代码置于do while循环之上


最后,创建一个新变量,该变量保持连续遇到密钥的次数。将其初始化为零,然后在循环中尝试跟踪连续遇到该键的次数

根据您的老师和/或书中的内容,您似乎需要使用数组或数组的一种形式来成功确定连续输入密钥的次数。下面是我将如何处理阵列:

import java.util.Scanner;
public class Consecutive 
{
    public static void main(String[] args) 
    {
        // Variable declaration
        int count = 0; 
        Scanner kbd = new Scanner(System.in);

        System.out.print("Enter a positive integer: ");
        int n = kbd.nextInt();

        System.out.print("Now, enter " + n + " integer(s), of any kind: ");    
        int[] ints = new int[n]; // Declares an array with length "n" to store the numbers

        for (int i = 0; i < n; i++)
        {
            if (kbd.hasNextInt()) // Checks to make sure they are entering integer.
                ints[i] = kbd.nextInt();
            else
            {
                i--; // Keeps increment the same unless you have a correct input
                kbd.next(); // Clears the scanner so you can check the next thing entered.
            }
        }

        System.out.println("Now enter one integer, the key: ");
        int key = kbd.nextInt();

        for (int i = 0; i < ints.length; i++)
        {
            if (ints[i] == key) // Checks to see what value in array is equal to key
                count++; // If it's equal add to count.
        }
        System.out.println("The key was entered " + count + " times."); // Display how many times key in array
    }
}
要为数组赋值,只需键入索引值(可以将其视为数组中保存值的插槽),例如:

int[] oneTwoThree = new int[3];
oneTwoThree[0] = 1;
oneTwoThree[1] = 2;
oneTwoThree[2] = 3;
请注意,在使用数组时,索引值从
0
开始,然后转到
length-1
。在数组中声明值的另一种方法如下:

type[] variableName = new type[optionalLength];
int[] oneTwoThree = new int[3];
for (int i = 0; i < oneTwoThree.length; i++)
    oneTwoThree[i] = i + 1; // i goes from 0 to 2, and we are setting oneTwoThree from one to three.
int[]onetwother=newint[3];
for(int i=0;i
当您第一次开始编码时,解决这些问题的一个好方法是将算法编写为“psuedocode”。这意味着将其写成一种英语,这样您就可以专注于解决方案,而不必担心语言语法

例如,下面是一些用于计算列表中特定项的最长连续序列的伪代码。希望它能给你一些开始的东西

set longestChain to 0
set currentChain to 0
for all the items in the list
    if item equals key 
        increment currentChain
        if currentChain is greater than longestChain
            set longestChain to currentChain
    otherwise
        set currentChain to 0
本质上,它查看每个项目,如果它等于键,则增加一个计数器。只要一个项目不等于键,它就会将计数器设置回零。同时,它记录了迄今为止最长的系列


如果您在转换代码时遇到问题,请告诉我。

我可以看出您是一名初学者。你知道数组吗?如果你这样做了,你应该首先创建一个大小为“n”的int数组,并在继续之前将用户给定的int值存储在该数组中。是的,我是初学者,不,我对数组了解不多。恐怕我不知道如何将你的建议转化为有用的代码。很抱歉,这不是一个我无法评论的答案,因为rep:/我是新来的,哈哈,是不是有其他的评论方式,还是我遗漏了什么?这不是整个do while语句的目的吗?此外,问题下方应该有蓝色的“添加注释”。do while语句是“现在输入一个整数-密钥”,因此程序将继续要求用户输入一个密钥,而any==n.length(在得到50个代表之前,我无法对您的问题进行注释)以下语句将创建一个长度为n的整数数组,即[a,b,c…n] ..... int[]数字=新的int[n];“那个数组对我不起作用,表达式的开始是非法的,”它说。你能解释一下这里如何使用数组吗?嗯,我明白了。所以我应该去掉整个do-while语句吗?@Beatz这取决于,如果你不使用数组,你必须使用一个循环来跟踪用户的输入。让我们一步一步来做吧。首先将n个整数接受到一个数组中,看看是否有效。然后在(1)中,在do while循环中初始化2个新变量int count=0和temp=0(2),直到遇到和key相同的元素increment temp。如果下一个元素不是键,则将temp和count的最大值保存在count中。如果您需要更多帮助,请告诉我。感谢您对代码的评论以及答案。两者都很有帮助。你有一个很棒的夜晚!你也一样。我在我的回答中发布了一些关于数组的更多信息以进行更多解释,如果我的回答对问题有帮助,您也可以将其标记为正确答案以结束问题:)谢谢,这确实有帮助。我的教授没有像我希望的那样彻底地复习数组。