
正文
java请编一段代码输入 编写一个java程序,输入你的名字
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
使用Java语言编写一段程序,实现从控制台输入并输出一个3行3列的数组。
import java.util.Scanner;
public class P
{
public static void main(String[] args)
{
int[][] a=new int[3][3];
Scanner sc = new Scanner(System.in);
System.out.println("请输入9个整数:");
for(int i=0;i3;i++)
{
for(int j=0;j3;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("你输入的3x3数组的数据是:");
for(int i=0;i3;i++)
{
for(int j=0;j3;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
相关问答
Q1: java 帮忙填一段代码
基本功能已经实现java请编一段代码输入,具体显示不舒服的地方自己再改改
import java.awt.*;
import java.awt.event.*;
public class shiyan2 extends Frame implements ActionListener {
CheckboxGroup cbg;// 定义单选按钮对象
Label lname, lsex, lbirth, lhobby, label, label1;
Choice cbirth, cbirth2, cbirth3;
TextArea tinfo;
TextField tname;
Checkbox mbox, wbox;// 定义两个复选框对象
Checkbox film, read, web, bc, art, tour, game, other;
Button button1, button2;
int arr[] = new int[12];
public shiyan2() {
super("用户个人信息");
setLayout(new FlowLayout());
// 姓名
label = new Label("请输入您的个人资料java请编一段代码输入,完成后单击确定。");
add(label);
lname = new Label("姓名:");
add(lname);
tname = new TextField(8);
add(tname);
// 性别
lsex = new Label("性别:");
add(lsex);
cbg = new CheckboxGroup();
mbox = new Checkbox("男", cbg, true);
wbox = new Checkbox("女", cbg, false);
add(mbox);
add(wbox);
// 生日
lbirth = new Label("生日:");
add(lbirth);
cbirth = new Choice();
cbirth2 = new Choice();
cbirth3 = new Choice();
cbirth.add("1988");
cbirth.add("1989");
cbirth.select(0);
cbirth2.add("1");
cbirth2.add("12");
cbirth2.select(0);
cbirth3.add("1988");
cbirth3.add("1989");
cbirth3.select(0);
add(cbirth);
add(cbirth2);
add(cbirth3);
// 爱好
lhobby = new Label("爱好:");
add(lhobby);
film = new Checkbox("电影");
add(film);
read = new Checkbox("阅读");
add(read);
web = new Checkbox("网络");
add(web);
bc = new Checkbox("编程");
add(bc);
add(new Label(" "));
art = new Checkbox("艺术");
add(art);
tour = new Checkbox("旅游");
add(tour);
game = new Checkbox("游戏");
add(game);
other = new Checkbox("其java请编一段代码输入他");
add(other);
// 输出
tinfo = new TextArea(5, 30);
add(tinfo);
button1 = new Button("确定");
add(button1);
button1.addActionListener(this);
button2 = new Button("退出");
add(button2);
button2.addActionListener(this);
// 单击标题栏关闭按钮java请编一段代码输入,退出程序
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(270, 310);
setLocationRelativeTo(null);// frame居中显示
setVisible(true);
}
public void actionPerformed(ActionEvent e){
StringBuffer hobby = new StringBuffer();
if(film.getState()==true){
hobby.append(film.getLabel()+" ");
}
if(read.getState()==true){
hobby.append(read.getLabel()+" ");
}
if(web.getState()==true){
hobby.append(web.getLabel()+" ");
}
if(art.getState()==true){
hobby.append(art.getLabel()+" ");
}
if(bc.getState()==true){
hobby.append(bc.getLabel()+" ");
}
if(tour.getState()==true){
hobby.append(tour.getLabel()+" ");
}
if(game.getState()==true){
hobby.append(game.getLabel()+" ");
}
if(other.getState()==true){
hobby.append(other.getLabel()+"");
}
if(e.getSource()==button1){
tinfo.setText(tname.getText()+
"性别:"+cbg.getSelectedCheckbox().getLabel()+"\n"
+"生日:"+cbirth.getSelectedItem()+"年"+cbirth2.getSelectedItem()+"月"+cbirth3.getSelectedItem()+"日"+"\n"
+"爱好:"+hobby.toString());
}
else if(e.getSource()==button2){System.exit(0);}
}
public static void main(String[] args) {
shiyan2 t = new shiyan2();
}
}
Q2: java 编写一个程序,输入3个整数,然后程序将对这三个整数按照从大到小进行排列
可以实现比较器Comparator来定制排序方案,同时使用Colletions.sort的方式进行排序,代码如下:
public void sortDesc(ListLong s){
Collections.sort(s, new ComparatorLong() {
public int compare(Long o1, Long o2) {
Long result = o2 - o1;
return result.intValue();
}
});
s.forEach(item-{
System.out.print(item +" ");
});
}
同时常用的比较排序算法主要有:冒泡排序,选择排序,插入排序,归并排序,堆排序,快速排序等。
java的冒泡排序实现如下:
public static void bubbleSort(int []arr) { for(int i =0;iarr.length-1;i++) { for(int j=0;jarr.length-i-1;j++) {//-1为了防止溢出 if(arr[j]arr[j+1]) { int temp = arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } }
还有非比较排序,时间复杂度可以达到O(n),主要有:计数排序,基数排序,桶排序等。
Q3: 用Java语言,编写一个程序,用户输入任意一个字符串,显示它的长度和第一个字符?
按照你的要求编写的Java程序如下
import java.util.Scanner;
public class A {
public static void main(String[] args) {
System.out.println("请输入任意一个字符串:");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.println(s+"的长度为"+s.length());
System.out.println(s+"的第一个字符为"+s.charAt(0));
sc.close();
}
}
Q4: 用java编写一个程序,实现键盘上输入一个整数n。
import java.util.Scanner;
public class InputN {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入一个数:");
int n = scan.nextInt();
int sum = 0;
if (n = 1) {
System.out.println("整数n应该为大于1的正整数");
} else {
for (int i = 0; i = n; i++) {
sum += i;
}
System.out.println("你输入的数为:" + n + "\t" + "1到" + n + "的整数和为:" + sum);
}
scan.close();
}
}
Q5: 编写一个JAVA程序 输入自己的姓名和年龄并输出 怎么写
在main里写
String name;
String age;
Scanner sc=new Scanner(System.in);
System.out.println("输入姓名");
name=sc.nextLine();
System.out.println("输入年龄");
age=sc.nextLine();
System.out.println("姓名"+name);
System.out.println("年龄"+age);
sc.close();
扩展资料:
JAVA程序的语言特点:
1.简单性
Java看起来设计得很像C++,但是为了使语言小和容易熟悉,设计者们把C++语言中许多可用的特征去掉了,这些特征是一般程序员很少使用的。例如,Java不支持go to语句,代之以提供break和continue语句以及异常处理。
Java还剔除了C++的操作符过载(overload)和多继承特征,并且不使用主文件,免去了预处理程序。因为Java没有结构,数组和串都是对象,所以不需要指针。
Java能够自动处理对象的引用和间接引用,实现自动的无用单元收集,使用户不必为存储管理问题烦恼,能更多的时间和精力花在研发上。
2.面向对象
Java是一个面向对象的语言。对程序员来说,这意味着要注意应中的数据和操纵数据的方法(method),而不是严格地用过程来思考。在一个面向对象的系统中,类(class)是数据和操作数据的方法的集合。
数据和方法一起描述对象(object)的状态和行为。每一对象是其状态和行为的封装。类是按一定体系和层次安排的,使得子类可以从超类继承行为。在这个类层次体系中有一个根类,它是具有一般行为的类。Java程序是用类来组织的。
java请编一段代码输入的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于编写一个java程序,输入你的名字、java请编一段代码输入的信息别忘了在本站进行查找喔。







