Java基础(Scanner键盘输入练习2)

题目:
键盘输入三个int数字,然后求出其中的最大值。

思路:
1.既然是键盘输入,肯定需要用到Scanner
2.Scanner三个步骤:导包、创建、使用nextInt()方法
3.既然是三个数字,那么调用三次nextInt()方法,得到三个int变量
4.无法同时判断三个数字谁最大,,应该转换为两个步骤,
(1)首先判断前两个数当中谁最大,拿到二者的最大值
(2)拿到二者最大值后和第三个数字进行比较,得到三个数字当中
的最大值。
5.打印最终结果。
代码如下:

package 类和对象;
import java.util.Scanner; // 1.导包
public class Demo05 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("请输入第一个数字:");
        int a = sc.nextInt();

        System.out.println("请输入第二个数字:");
        int b = sc.nextInt();

        System.out.println("请输入第三个数字:");
        int c = sc.nextInt();

        //首先比较前两个数字取最大值
        int temp = a > b ? a : b;
        int max = temp > c ? temp : c;
        System.out.println("您输入的三个数字中最大的数为:"+max);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 编写一个程序,要求用户输入两个整数,并输出它们的和、差、积、商和余数。 ```java import java.util.Scanner; public class InputDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入第一个整数:"); int a = sc.nextInt(); System.out.print("请输入第二个整数:"); int b = sc.nextInt(); System.out.println("它们的和是:" + (a + b)); System.out.println("它们的差是:" + (a - b)); System.out.println("它们的积是:" + (a * b)); System.out.println("它们的商是:" + (a / b)); System.out.println("它们的余数是:" + (a % b)); } } ``` 2. 编写一个程序,要求用户输入一个圆的半径,并输出它的周长和面积。 ```java import java.util.Scanner; public class CircleDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入圆的半径:"); double radius = sc.nextDouble(); double perimeter = 2 * Math.PI * radius; double area = Math.PI * radius * radius; System.out.println("它的周长是:" + perimeter); System.out.println("它的面积是:" + area); } } ``` 3. 编写一个程序,要求用户输入三个整数,并输出它们的和、平均数和乘积。 ```java import java.util.Scanner; public class MathDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入第一个整数:"); int a = sc.nextInt(); System.out.print("请输入第二个整数:"); int b = sc.nextInt(); System.out.print("请输入第三个整数:"); int c = sc.nextInt(); int sum = a + b + c; double average = (a + b + c) / 3.0; int product = a * b * c; System.out.println("它们的和是:" + sum); System.out.println("它们的平均数是:" + average); System.out.println("它们的乘积是:" + product); } } ``` 4. 编写一个程序,要求用户输入一个四位整数,并将它各位数字反转输出。 ```java import java.util.Scanner; public class ReverseDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入一个四位整数:"); int num = sc.nextInt(); int digit1 = num / 1000; // 取千位数字 int digit2 = num % 1000 / 100; // 取百位数字 int digit3 = num % 100 / 10; // 取十位数字 int digit4 = num % 10; // 取个位数字 System.out.println("反转后的数字是:" + digit4 + digit3 + digit2 + digit1); } } ``` 5. 编写一个程序,要求用户输入一个字符串,并输出该字符串的长度、第一个字符、最后一个字符和中间字符。 ```java import java.util.Scanner; public class StringDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入一个字符串:"); String str = sc.nextLine(); int length = str.length(); // 字符串长度 char firstChar = str.charAt(0); // 第一个字符 char lastChar = str.charAt(length-1); // 最后一个字符 // 中间字符(当字符串长度为偶数时,返回中间的两个字符) char middleChar = length % 2 == 0 ? str.charAt(length/2-1) : str.charAt(length/2); System.out.println("字符串长度为:" + length); System.out.println("第一个字符是:" + firstChar); System.out.println("最后一个字符是:" + lastChar); System.out.println("中间字符是:" + middleChar); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值