「java函数如何循环输出」java的循环输入语句怎么写

博主:adminadmin 2023-01-17 15:12:07 395

今天给各位分享java函数如何循环输出的知识,其中也会对java的循环输入语句怎么写进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java如何把循环遍历结果输出到文本文档?

首先,啊,我的眼睛!请学会截图,你的这三张图我一张都看不清!

然后我想了一下你的目的,你现在有一个学生信息数组,你是要把他们写到一个文件里是吧,这个过程叫做数据序列化或者持久化(其实文件中保存成json串或xml的形式更容易阅读数据和反序列化)因为看不清你的程序,所以我举了个例子给你看下,给你些思路。

我定义一个学生类,包括姓名和分数两个属性:

之后在main函数中构造拥有三个学生的学生信息数组:

然后使用FileOutputStream、OutputStreamWriter、BufferedWriter完成文件的写入:

流的使用方式我就不多说了,记住流一定要关闭,最好实在finally块中进行,另外先打开的流后关闭。

主要看写文件内容的部分:

其实就是循环数组,使用bufferWrite的write方法,将我们的数据按照想要的格式弄成字符串,建议使用StringBuilder来构建文件字符串内容,我这里偷懒了直接用的+来操作,最后适时地换行。

最终生成的文件内容为:

完整main函数代码:

public static void main(String[] args) throws Exception {

Student s1 = new Student("张三", 90);

Student s2 = new Student("李四", 59);

Student s3 = new Student("王五", 85);

Student[] students = new Student[]{s1, s2, s3};

String filePath = "d:\\student.txt";

FileOutputStream fileOutputStream = null;

OutputStreamWriter outputStreamWriter = null;

BufferedWriter bufferedWriter = null;

try {

fileOutputStream = new FileOutputStream(filePath);

outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");

bufferedWriter = new BufferedWriter(outputStreamWriter);

for (int i = 0; i students.length; i++) {

bufferedWriter.write(students[i].getName() + " " + students[i].getGrade());

if (i students.length - 1) {

bufferedWriter.newLine();

}

}

} finally {

if (bufferedWriter != null) {

bufferedWriter.close();

}

if (outputStreamWriter != null) {

outputStreamWriter.close();

}

if (fileOutputStream != null) {

fileOutputStream.close();

}

}

}

java循环输出100次java

方法一:while循环

public class Test{

public static void main(String[] args){

int i=1;

while(i101){ //循环100次

System.out.print("java:"+i+"次"); //每次循环打印一次java

System.out.println(); //打印空行

i++;

}

}

方法二:for循环

public class Test{

public static void main(String[] args){

for (int i=0; i100; i++ ){ //循环100次

System.out.print("java:"+(i+1)+"次"); //每次循环打印一次java

}

System.out.println(); //打印空行

}

}

java for循环输出

但是 你的程序那样的话 是一个循环 你的程序输出的结果是这样的:

这是i的值0

这是i的值1

这是i的值2

这是j的值0

这是j的值1

这是j的值2

这是j的值3

这是j的值4

程序:

public class Test{

public static void main(String args[]){

int a=3,b=5;

for(int i=0;ia;i++)

{System.out.println("这是i的值"+i); //这里你多写了一个括号

}

for(int j=0;jb;j++)

{

System.out.println("这是j的值"+j);//应该是小写的j

}

}

}

java如何循环输出数组?

有两种方法:

1. 使用三层循环遍历多维数组

public class Ransack {

public static void main(String[] args) {

int array[][][] = new int[][][]{ // 创建并初始化数组

{ { 1, 2, 3 }, { 4, 5, 6 } },

{ { 7, 8, 9 }, { 10, 11, 12 } },

{ { 13, 14, 15 }, { 16, 17, 18 } }

};

array[1][0][0] = 97; // 改变指定数组元素

for (int i = 0; i array.length; i++) { // 遍历数组

for (int j = 0; j array[0].length; j++) {

for (int k = 0; k array[0][0].length; k++) {

System.out.print(array[i][j][k] + "\t");

}

System.out.println(); // 输出一维数组后换行

}

}

}

2.使用foreach 遍历三维数组

public class ForEachRansack {

public static void main(String[] args) {

int array[][][] = new int[][][]{ // 创建并初始化数组

{ { 1, 2, 3 }, { 4, 5, 6 } },

{ { 7, 8, 9 }, { 10, 11, 12 } },

{ { 13, 14, 15 }, { 16, 17, 18 } }

};

for (int[][] is : array) { // 遍历数组

for (int[] is2 : is) {

for (int i : is2) {

System.out.print(i + "\t");

}

System.out.println(); // 输出一维数组后换行

}

}

}

}

java如何让数组里的字符串循环输出

通过for循环后把数组中的字符串输出

1、定义字符串数组

String arr[] = new String[]{"a","b","c"};//定义一个字符串数组arr

2、循环数组

for(int i=0;iarr.length;i++){//通过arr.length获取字符串数组长度

   System.out.println(arr[i]);//循环输出字符串数组元素

}

关于Java函数for循环

public class Demo{

public static void main(String args[]){

for(int i=1; i=4;i++){

for(int j=1;j=i;j++){

System.out.print("*");

}

System.out.println();

}

}

}

此代码有错,我已修改成如上。现在来说代码的意思:

i表示的是行数(i = 1即表示为第一行),j表示的是每行输出的星号个数,

因为你每行的星号数和行数相等,所以要有条件 "j = i" 当输入完成

星号的指令后,程序控制输出换行,然后继续输入第二行的星号。

关于java函数如何循环输出和java的循环输入语句怎么写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。