我已经完成了代码,但有人可以帮我查一下。我无法输出样本所假设的确切输出。我的代码无法识别名称之间的正确间距并确定字母的确切位置。以及正确计算字母数量而不会产生其他错误。
程序1:提示用户输入任何全名(第一个中间姓)。不要为O' Reilly,Van Helsing,de Ville等姓氏打扰。然后输出以下内容:
全名的长度。 中间名的长度。 这个名字的三个首字母。 全部大写的名称。
SAMPLE OUT:
输入名字中间名和姓氏
Peggy Sue Palmer
姓名长度:16个字符
中间名长度:3个字符
您的姓名缩写是PSP
PEGGY SUE PALMER
我的代码:
import java.util.Scanner;
public class Program2_1 {
//private static String name;
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
System.out.println("Enter a first name, middle name, and surname:");
String first_name = user_input.next();
String second_name = user_input.next();
String surname = user_input.next();
System.out.println("Length of your name: " + first_name.length() + second_name.length() + surname.length() + " characters");
System.out.println("Length of your middle name: " + second_name.length() + " characters");
System.out.println("Your initials are " + first_name.charAt(0)+ second_name.charAt(0)+ surname.charAt(0));
System.out.println(first_name + second_name + surname);
}
}
程序2:编写一个程序,生成两个随机整数,范围均为50到100,包括50和100。使用Math类。打印两个整数然后显示两个整数之间的正差异,但使用选择。不要使用Math类的绝对值方法。
我的代码:
import java.lang.Math;
public class Program2_2 {
public static void main(String[] args) {
//int num = 50(int)(Math.random()* 51);
int x = (int)(50 + Math.random()* 51);
System.out.println("Integer one: " + x);
int y = (int)(50 + Math.random()*51);
System.out.println("Integer two: " + y);
int z = (x - y);
if (y > x)
z = (y - x);
System.out.println("The positive difference between both integer: " + z);
}
}
答案 0 :(得分:1)
第二个代码中的一个问题是你需要强制转换if语句。
if (y > x){
z = (y - x);
System.out.println("The positive difference between both integer: " + z);
}