Java在while循环中存储值
我是Java的初学者,我有一个关于循环的作业。我知道这个问题对你来说可能听起来很基本,但我找不到一个可以理解的答案。我的代码基本上看起来像Java在while循环中存储值,java,drjava,Java,Drjava,我是Java的初学者,我有一个关于循环的作业。我知道这个问题对你来说可能听起来很基本,但我找不到一个可以理解的答案。我的代码基本上看起来像 import java.util.Scanner; public class Histogram { public static void main( String[] args) { Scanner scan = new Scanner( System.in); // Constant final String CH
import java.util.Scanner;
public class Histogram
{
public static void main( String[] args)
{
Scanner scan = new Scanner( System.in);
// Constant
final String CH = "*";
// Variables
int number;
int count;
int start;
int width;
int line;
String output;
// Program Code
count = 0;
number = 0;
start = 1;
width = 0;
line = 0;
output = "";
// Creating the loop
while ( start == 1)
{
System.out.println( "Enter the numbers");
number = scan.nextInt();
if ( number < 0 )
{
start = 0;
}
else
{
while ( width < number)
{
output = CH + " " ;
width = width + 1 ;
System.out.print(output);
}
width = 0;
}
}
}
}
import java.util.Scanner;
公共类直方图
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
//不变的
最终字符串CH=“*”;
//变数
整数;
整数计数;
int启动;
整数宽度;
内线;
字符串输出;
//程序代码
计数=0;
数字=0;
开始=1;
宽度=0;
直线=0;
输出=”;
//创建循环
while(start==1)
{
System.out.println(“输入数字”);
number=scan.nextInt();
如果(数字<0)
{
开始=0;
}
其他的
{
while(宽度<数量)
{
输出=CH+“”;
宽度=宽度+1;
系统输出打印(输出);
}
宽度=0;
}
}
}
}
这运行得很好,但每次用户输入后都会打印星号的输出。我想存储每个循环的输出,并在输入负整数时将它们一起打印。我不知道用户将输入多少输出,如何存储这些输出?
简而言之,我希望有以下输出
输入数字:3 5 6 4-1//数字是用户输入
三,***
5 *****
6 ******
4****使用列表,例如ArrayList,而不是数组,因为大小可以动态增长 比如说,
List<Integer> ints = new ArrayList<>();
ints.add(5);
ints.add(2);
ints.add(7);
for(Integer i : ints){
System.out.println(i);
}
List ints=new ArrayList();
加入(5);
内加(2);
内加(7);
for(整数i:int){
系统输出打印LN(i);
}
如果不需要重复,可以使用集合。使用列表,例如ArrayList,而不是数组,因为大小可以动态增长 比如说,
List<Integer> ints = new ArrayList<>();
ints.add(5);
ints.add(2);
ints.add(7);
for(Integer i : ints){
System.out.println(i);
}
List ints=new ArrayList();
加入(5);
内加(2);
内加(7);
for(整数i:int){
系统输出打印LN(i);
}
如果不需要重复,可以使用集合。您只需在while范围之外创建一个变量(如
String
或StringBuilder
(首选)),而不是直接打印结果,您可以将任何输出附加到此变量,并在最后打印它
使用这种方法,您基本上是收集整个输出并一次全部打印出来
...
//create a output variable outside while loop. You can also use simple String
StringBuilder out = new StringBuilder();
while ( start == 1)
{
System.out.println( "Enter the numbers");
number = scan.nextInt();
if ( number < 0 )
{
start = 0;
}
else
{
while ( width < number)
{
output = CH + " " ;
width = width + 1 ;
//instead of directly printing, you can append your output to out variable.
out.append(output);
}
width = 0;
}
}
//after coming out of the loop, you can now print it
System.out.print(out);
...
。。。
//在while循环外部创建一个输出变量。也可以使用简单字符串
StringBuilder out=新的StringBuilder();
while(start==1)
{
System.out.println(“输入数字”);
number=scan.nextInt();
如果(数字<0)
{
开始=0;
}
其他的
{
while(宽度<数量)
{
输出=CH+“”;
宽度=宽度+1;
//您可以将输出附加到out变量,而不是直接打印。
out.append(输出);
}
宽度=0;
}
}
//退出循环后,现在可以打印它了
系统输出打印(输出);
...
您只需在while范围之外创建一个变量(如String
或StringBuilder
(首选)),而不是直接打印结果,您需要将任何输出附加到此变量,并在最后打印它
使用这种方法,您基本上是收集整个输出并一次全部打印出来
...
//create a output variable outside while loop. You can also use simple String
StringBuilder out = new StringBuilder();
while ( start == 1)
{
System.out.println( "Enter the numbers");
number = scan.nextInt();
if ( number < 0 )
{
start = 0;
}
else
{
while ( width < number)
{
output = CH + " " ;
width = width + 1 ;
//instead of directly printing, you can append your output to out variable.
out.append(output);
}
width = 0;
}
}
//after coming out of the loop, you can now print it
System.out.print(out);
...
。。。
//在while循环外部创建一个输出变量。也可以使用简单字符串
StringBuilder out=新的StringBuilder();
while(start==1)
{
System.out.println(“输入数字”);
number=scan.nextInt();
如果(数字<0)
{
开始=0;
}
其他的
{
while(宽度<数量)
{
输出=CH+“”;
宽度=宽度+1;
//您可以将输出附加到out变量,而不是直接打印。
out.append(输出);
}
宽度=0;
}
}
//退出循环后,现在可以打印它了
系统输出打印(输出);
...
在“else”中,您可以只存储输入的数字:
List<Integer> myList = new ArrayList<>(); //previously defined
(...)
else {
myList.add(number);
}
List myList=new ArrayList()//先前定义的
(...)
否则{
myList.add(编号);
}
然后在while循环之外,一旦填充了arraylist:
for(int i=0;i<myList.size();i++)
{
for(int j=0;j<myList.get(i);j++)
{
System.out.print("*");
}
System.out.print("\n");
}
for(int i=0;i在“else”中,您可以只存储输入的数字:
List<Integer> myList = new ArrayList<>(); //previously defined
(...)
else {
myList.add(number);
}
List myList=new ArrayList();//以前定义
(...)
否则{
myList.add(编号);
}
然后在while循环之外,一旦填充了arraylist:
for(int i=0;i<myList.size();i++)
{
for(int j=0;j<myList.get(i);j++)
{
System.out.print("*");
}
System.out.print("\n");
}
for(inti=0;i这是答案:
import java.util.Scanner;
public class Histogram
{
public static void main( String[] args)
{
Scanner scan = new Scanner( System.in);
// Constant
final String CH = "*";
int count = 0;
int number = 0;
int start = 1;
int width = 0;
int line = 0;
String output = "";
String store="";
// Creating the loop
while ( start == 1)
{
System.out.println( "Enter the numbers");
number = scan.nextInt();
if ( number < 0 )
{
start = 0;
}
else
{
while ( width < number)
{
output = CH + " " ;
width = width + 1 ;
store+=output;
}
width = 0;
}
}
System.out.print(store);
}
}
import java.util.Scanner;
公共类直方图
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
//不变的
最终字符串CH=“*”;
整数计数=0;
整数=0;
int start=1;
整数宽度=0;
内线=0;
字符串输出=”;
字符串存储=”;
//创建循环
while(start==1)
{
System.out.println(“输入数字”);
number=scan.nextInt();
如果(数字<0)
{
开始=0;
}
其他的
{
while(宽度<数量)
{
输出=CH+“”;
宽度=宽度+1;
存储+=输出;
}
宽度=0;
}
}
系统输出打印(存储);
}
}
答案如下:
import java.util.Scanner;
public class Histogram
{
public static void main( String[] args)
{
Scanner scan = new Scanner( System.in);
// Constant
final String CH = "*";
int count = 0;
int number = 0;
int start = 1;
int width = 0;
int line = 0;
String output = "";
String store="";
// Creating the loop
while ( start == 1)
{
System.out.println( "Enter the numbers");
number = scan.nextInt();
if ( number < 0 )
{
start = 0;
}
else
{
while ( width < number)
{
output = CH + " " ;
width = width + 1 ;
store+=output;
}
width = 0;
}
}
System.out.print(store);
}
}
import java.util.Scanner;
公共类直方图
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
//不变的
最终字符串CH=“*”;
整数计数=0;
整数=0;
int start=1;
整数宽度=0;
内线=0;
字符串输出=”;
字符串存储=”;
//创建循环
while(start==1)
{
System.out.println(“输入数字”);
number=scan.nextInt();
如果(数字<0)
{
开始=0;
}
其他的
{
while(宽度<数量)
{
输出=CH+“”;