我承认,这确实是一个学校项目。这是问题本身,因为我觉得它太复杂而无法解释:
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());
}
}
我知道这不是最伟大的东西。任何帮助都将非常感激。你有自己的bangin&#39;晚上。
答案 0 :(得分:2)
根据您的教师和/或书籍所写的内容,您似乎需要使用数组或其中的一种形式来成功确定连续输入密钥的次数。以下是我对数组的处理方法:
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
}
}
要使用数组,首先必须使用以下形式为数组分配内存:
type[] variableName = new type[optionalLength];
要为数组赋值,只需键入索引值(您可以将其视为数组中包含值的插槽),例如:
int[] oneTwoThree = new int[3];
oneTwoThree[0] = 1;
oneTwoThree[1] = 2;
oneTwoThree[2] = 3;
请注意,在使用数组时,它从0
开始,并转到length - 1
获取索引值。在数组中声明值的另一种方法是:
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.
答案 1 :(得分:1)
System.out.print("Now, enter "+n+" integer(s), of any kind: ");
int any = kbd.nextInt();
在这句话中,你不接受n个数字。你只是接受一个号码给用户。数组是一种将相同数据类型的多个值存储在一起的方法。您可能希望在java中查找和读取数组。
另外,您在do-while循环中接受来自用户的密钥。你只需要输入一次密钥。所以你可能想把代码放在do-while循环之上。
最后,有一个新变量可以保持遇到连续键次数。将其初始化为零,现在循环尝试跟踪连续遇到密钥的次数。
答案 2 :(得分:0)
再详细说明一下,你已经在用户输入一个密钥时循环,但只有在输入所有数字后才能输入一次密钥?重新思考什么过程实际上需要重复,然后再问你是否卡住了:)
答案 3 :(得分:0)
首次开始编码时解决这些问题的好方法是将算法编写为'伪代码'。这意味着将其编写为英语类型,使您可以专注于解决方案,而无需担心语言语法。
例如,这里有一些用于计算列表中特定项目的最长连续系列的伪代码。希望它能给你一些开始的东西
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
基本上它会查看每个项目,如果它等于键,则递增计数器。只要一个项目不等于该键,它就会将计数器设置回零。同时它记录了迄今为止最长的系列。
如果您无法转换为代码,请与我们联系。