我已经完成了代码,但是有人能帮我检查一下吗?我在输出样本的准确输出时有困难。我的代码无法识别名称之间的适当间距,也无法确定字母的确切位置。以及正确计算字母的数量,而不产生其他错误。
程序1:提示用户输入任何全名(第一个中间姓)。不要费心使用O‘’Reilly、Van Helsing、de Ville等姓氏,然后输出以下内容:
全名的长度。中间名的长度。名字的三个首字母。大写的名字。
样本输出:
输入名、中间名和姓
佩吉·苏·帕尔默
您的姓名长度:16个字符
中间名的长度:3个字符
你的首字母是PSP
佩吉·苏·帕尔默
我的代码:
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之间,包括在内。使用数学类。打印两个整数,然后显示两个整数之间的正差,但使用选择。不要使用数学类的绝对值方法。
我的代码:
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);
}
}发布于 2017-03-24 01:55:01
第二个代码中的一个问题是您需要强制转换if语句。
if (y > x){
z = (y - x);
System.out.println("The positive difference between both integer: " + z);
} https://stackoverflow.com/questions/42990025
复制相似问题