写了一个代码,代码如下,可以进行参考
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、网站制作、成都外贸网站建设、宣城网络推广、微信小程序定制开发、宣城网络营销、宣城企业策划、宣城品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供宣城建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
public class sum {
public static void main(String[] args) {
//创建一个Scanner的对象input
Scanner input = new Scanner(System.in);
//提示用户输入数据
System.out.print("请输入一个整数");
//将输入的值赋给n
int n = input.nextInt();
//定义变量接收计算后的和
int sum = 0;
//利用循环进行求和
for (int i = 0; i = n; i++) {
sum+=i;
}
//输出最后的和
System.out.println("从0一直到"+n+"的所有整数的和是:"+sum);
}
}
你好,按照你的要求代码如下,可以直接运行,并给出了运行结果:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
// 获得用户输入
System.out.println("请输入一个数:");
Scanner s = new Scanner(System.in);
int i = s.nextInt();
s.close();
// 判断是否是三位数
if (i 100 || i 999) {
System.out.println("不是三位数");
return;
}
// 输入结果
String str = String.valueOf(i);
System.out.println("百位:" + str.charAt(0));
System.out.println("十位:" + str.charAt(1));
System.out.println("个位:" + str.charAt(2));
}
}
运行结果:
请输入一个数:
134
百位:1
十位:3
个位:4
最简单的java代码肯定就是这个了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!
import java.util.Comparator;
public class Student implements ComparableStudent {
private int no;
private String name;
private String sex;
private int roomNo;
private double score;
public Student(int no, String name, String sex, int roomNo, double score) {
this.no = no;
this.name = name;
this.sex = sex;
this.roomNo = roomNo;
this.score = score;
}
public Student(int no, String name, String sex, double score) {
this.no = no;
this.name = name;
this.sex = sex;
this.score = score;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getRoomNo() {
return roomNo;
}
public void setRoomNo(int roomNo) {
this.roomNo = roomNo;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public int compareTo(Student o) {
if (this.no o.no) return 1;
else if (this.no o.no) return -1;
else return 0;
}
@Override
public String toString() {
return "Student{" +
"no=" + no +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", roomNo=" + roomNo +
", score=" + score +
'}';
}
}
//性别比较器类
class SexComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getSex().compareTo(o2.getSex()) 0) return 1;
else if (o1.getSex().compareTo(o2.getSex()) 0) return -1;
else return 0;
}
}
//寝室号比较器类
class RoomNoComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getRoomNo() o2.getRoomNo()) return 1;
else if (o1.getRoomNo() o2.getRoomNo()) return -1;
else return 0;
}
}
//入学成绩比较器类
class ScoreComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getScore() o2.getScore()) return 1;
else if (o1.getScore() o2.getScore()) return -1;
else return 0;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student(1, "jack", "boy", 90);
Student s2 = new Student(5, "jack", "boy", 90);
Student s3 = new Student(4, "jack", "boy", 90);
Student s4 = new Student(2, "jack", "boy", 90);
ListStudent studentList=new ArrayList();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
Collections.sort(studentList);
System.out.println(Arrays.toString(studentList.toArray()));
}
}