JAVA输入输出语句以及a+b输入输出练习

一、在Java中,常用的输入输出语句包括以下几种:

1. 标准输出语句:
```java
System.out.println("Hello, World!"); // 输出并换行
System.out.print("Hello, World!"); // 输出但不换行
```

2. 标准输入语句:
```java
import java.util.Scanner; // 需要导入Scanner类

Scanner scanner = new Scanner(System.in); // 创建Scanner对象

System.out.print("请输入一个整数:");
int num = scanner.nextInt(); // 读取用户输入的整数
System.out.println("你输入的整数是:" + num);
```

//其中nextInt()是输入整数 nextLine()输入字符串 nextDouble()输入双精度数 next()输入字符串(以空格作为分隔符)。

3. 文件输入输出语句:
```java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

// 文件写入
File file = new File("example.txt");
try {
    FileWriter writer = new FileWriter(file);
    writer.write("Hello, World!");
    writer.close();
    System.out.println("文件写入成功!");
} catch (IOException e) {
    e.printStackTrace();
}

// 文件读取
try {
    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        System.out.println(line);
    }
    scanner.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
```

二、a+b输入输出练习

 代码如下:

import java.util.Scanner;

public class learn {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int a=sc.nextInt();//读入下一个整数
            int b=sc.nextInt();
            System.out.println(a+b);
        }
    }
}

 

 代码如下:

import java.util.Scanner;

public class learn1 {
    public static void main(String[] args ) {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        while(x>0){
            x--;
            int a=sc.nextInt();
            int b=sc.nextInt();
            System.out.println(a+b);
        }
    }
}

 代码如下:

import java.util.Scanner;

public class learn2 {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int a=sc.nextInt();
            int b=sc.nextInt();
            if(a==0&&b==0) {
                break;}
            System.out.println(a+b);
        }
    }
}

代码如下:

import java.util.Scanner;

public class learn3 {
    public static void main(String [] args) {
        Scanner sc=new Scanner(System.in); 
        int N=sc.nextInt();//输入N
        while(N!=0) {
            int sum=0;
            int[] nember=new int[N];
            for(int i=0;i<N;i++) {
                nember[i]=sc.nextInt();
                sum+=nember[i];
            }
            System.out.println(sum);
            N=sc.nextInt();
        }
    }
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ξ( ✿>◡❛)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值