如何修改程序并使用arraylist? [英] How to modify the program and use the arraylist?

查看:83
本文介绍了如何修改程序并使用arraylist?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Scanner;

public class Unique
{
// gets 5 unique numbers from the user
public static void main(String args[]) {
{
Scanner input = new Scanner( System.in );

/* Create an array of five elements*/ 
int count = 0; // number of uniques read
int entered = 0; // number of entered numbers

while( entered < numbers.length )
{
System.out.print( "Enter number: " );
/* Write code here to retrieve the input from the user */

// validate the input
/* Write an if statement that validates the input */ 
{
// flags whether this number already exists
boolean containsNumber = false;

// increment number of entered numbers
entered++;

/* Compare the user input to the unique numbers in the array using a for 
statement. If the number is unique, store new number */

/* add the user input to the array only if the number is not already 
in the array */
if ( !containsNumber )
{
/* Write code to add the number to the array and increment 
unique items input */
} // end if
else 
System.out.printf( "%d has already been entered\n",
number );
} // end if
else
System.out.println( "number must be between 10 and 100" );

// print the list of unique values
/* Write code to output the contents of the array */
} // end while 
} // end method Main
} // end class Unique









这是样本输出:



输入[10,100]中的数字:11

11

输入[10,100]中的数字]:22

11 22

输入[10,100]中的数字:33

11 22 33

输入[10,100]中的数字:11

11已经输入

11 22 33

输入[10,100]中的数字:44

11 22 33 44





En [10,100]中的数字:10

10

输入[10,100]中的数字:30

10 30

输入[10,100]中的数字:5

数字必须介于10和100之间

10 30

在[输入数字] 10,100]:50

10 30 50

输入[10,100]中的数字:70

10 30 50 70

输入[10,100]中的数字:100

10 30 50 70 100




我尝试过的事情:



我想弄清楚它很长一段时间但仍然无法正常工作...有谁可以帮助我吗?我需要立即...

推荐答案

运行它并找出第一个错误是什么。修复它,然后转到下一个错误并修复它。继续此过程,直到完成。
Run it and figure out what the first error is. Fix it, then go to the next error and fix that. Continue this process until you are done.


这篇关于如何修改程序并使用arraylist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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