「java简单程序题」java简答题
本篇文章给大家谈谈java简单程序题,以及java简答题对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
简单的java程序题
public
class
student{
private
string
stuid;
private
string
stuname;
private
char
stusex;
private
int
stuage;
/**
*以下是各属性的getter/setter方法
*/
public
void
setstuid(string
stuid){
this.stuid=stuid;
}
public
string
getstuid(){
return
stuid;
}
public
void
setstuname(string
stuname){
this.stuname=stuname;
}
public
string
getstuname(){
return
stuname;
}
public
void
setstusex(char
stusex){
this.stusex=stusex;
}
public
char
getstusex(){
return
stusex;
}
public
void
setstuage(int
stuage){
this.stuage=stuage;
}
public
int
getstuage(){
return
stuage;
}
/**
*构造方法,构造学生信息
*/
public
student(string
stuid,string
stuname,char
stusex,int
stuage){
this.stuid=stuid;
this.stuname=stuname;
this.stusex=stusex;
this.stuage=stuage;
}
public
string
tostring(){//覆盖该类的tostring()方法
stringbuffer
buff=new
stringbuffer();
buff.append("学号:"+stuid);
buff.append("\n姓名:"+stuname);
buff.append("\n性别:"+stusex);
buff.append("\n年龄:"+stuage);
return
buff.tostring();
}
public
static
void
main(string[]
args){
student
stu=new
student("1000","zhangsan",'男',18);
system.out.println
(stu);//打印学生信息
system.out.println
("--修改姓名结果--");
stu.setstuname("lisi");
system.out.println
(stu);
}
}
JAVA程序设计题(很简单的)
你的题有很多错误,我给你改了一下。
1.设变量i和j的定义如下,试分别计算下列表达式的值:
int i=1; double d=1.0;
1题 35/4 [8]
2题 46%9+4*4-2 [15]
3题 45+43%5*(23*3%2)[48]
4题 45+45*50%i-- [45]
5题 45+45*50%(i--) [45]
6题 1.5*3+(++d) [6.5]
7题 1.5*3+d++ [5.5]
8题 i+=3/i+3 [7]
程序阅读题
1给定如下代码,写出程序运行结果
class Example{
public static void main(String arges[]){
int i=0;
do{
System.out.println("doing it for i is:"+i);
}while(--i0);
System.out.println("finish");
}
}
结果如下:
doing it for i is:0
finish
2 阅读程序段写出执行结果
for(int k=1;k=5;k++){
if(k4)break;
System.out.println("k="+k);
}
结果:
k=1
k=2
k=3
k=4
3试写出下列程序段循环的运行结果
int i=1;
while(i10)
if(i++%2==0)
System.out.println(i);
结果:
3
5
7
9
操作题
求1!+2!+...+10!
public static void main(String arges[]){
long sum = 0;
for(int i = 1; i = 10; i++) {
long s = 1;
for(int j = 1; j = i; j++) {
s *= j;
}
sum += s;
}
System.out.println("sum = " + sum);
}
求100之内的所有“完数”。完数是指等于它的因子和的数。例如:6=1+2+3,6=1*2*3,则6是一个完数
public class wanshu{
public static void main(String[] args) {
for(int i = 1; i = 100; i++) {
if(fun(i)) {
System.out.println(i);
}
}
}
public static boolean fun(int num) {
int sum = 0;
for(int i = 1; i num; i++) {
if(num % i == 0) {
sum += i;
}
}
return num == sum;
}
}
一道简单的java编程题?
import java.text.ParseException;
import java.text.SimpleDateFormat;
//日期类
public class Date {
private String year;
private String month;
private String day;
public Date(String year, String month, String day) {
this.year = year;
this.month = month;
this.day = day;
}
public void format(){
System.out.println(day + "/" + month + "/" + year);
}
public void calculate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
try {
java.util.Date startDate = sdf.parse(year + "/" + "01" + "/" + "01");
java.util.Date inputDate = sdf.parse(year + "/" + month + "/" + day);
long resultDay = (inputDate.getTime() - startDate.getTime())/(24 * 1000 * 60 * 60);
System.out.println("第" + (resultDay + 1) + "天");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
//测试类
public class Test {
public static void main(String[] args) {
Date date1 = new Date("2020","04","11");
Date date2 = new Date("2020","01","02");
date1.format();
date1.calculate();
date2.format();
date2.calculate();
}
}
Java 程序设计题 急急急
代码如下
class Box{
private int width;
private int length;
private int height;
public Box(int width,int length,int height){
this.width = width;
this.length = length;
this.height = height;
}
public void showBox(){
System.out.println("盒子的width、length、height分别为"+width+","+length+","+height);
}
}
如果有帮助到你,请点击采纳
JAVA编程的几个简单题目
第一个:
import java.util.Scanner;
import java.util.*;
public class Validate
{
private int n;
/*count_6、count_7、count_8 用来记录收敛那个数字的个数,在这里我记录只要他出现了10次我就认为他收敛与他了
* 还没想到更好的办法,如果不设置这个,就会出现栈溢出,递归不出来了!不过可以看到结果输出结果确实是对的
*/
private int count_6;
private int count_7;
private int count_8;
private StackInteger stack= new StackInteger();//栈用来存放素因子
public void scan()
{
Scanner scan = new Scanner(System.in);
try{
n = scan.nextInt();
}catch(NumberFormatException ne){
System.out.println(ne.getMessage());
}
}
public boolean isPrime(int n)
{
if(n2 0 == n%2)//是大于2偶数
{
return false;
}else{
for(int i=3; in; i +=2)
{
if(0 == n%i)
return false;
}
return true;
}
}
public void analyze(int n)
{
if(isPrime(n))
{
stack.push(n);
return;
}
if(0 == n%2){
stack.push(2);
n = n/2;
analyze(n);
}else{
for(int i=3; in; i +=2)
{
if(isPrime(i) 0 == n%i)
{
stack.push(i);
n = n/i;
analyze(n);
}
}
}
}
public void mySort()
{
check(n);
}
public void check(int m)
{
if(isPrime(m)){
m++;
}
else{
analyze(m);
m = 0;
while(!stack.empty())
{
int k = stack.pop().intValue();
m += k;
}
stack.clear();
m++;
}
if(m == 6 || m == 7 || m == 8)
{
if(6 == m)
{
count_6++;
System.out.println("m = " + m);
}else if(7 == m){
count_7++;
System.out.println("m = " + m);
}else if(8 == m){
count_8++;
System.out.println("m = " + m);
}
}
if(count_6 10 || count_7 10 || count_8 10)
{
return;
}
check(m);
}
public static void main(String[] args)
{
Validate v = new Validate();
v.scan();
v.mySort();
}
}
第二个:
import java.util.Scanner;
class MyException extends Exception
{
public MyException(String msg)
{
super(msg);
}
}
public class MyExceptionTest {
public int scan()
{
int a = 0;
Scanner scan = new Scanner(System.in);
try{
a = scan.nextInt();
}catch(NumberFormatException ne){
System.out.println(ne.getMessage());
}
return a;
}
public int cal(int a, int b) throws MyException
{
if(b==0) throw new MyException("自定义异常: 输入的第二个数为0");
else if(a/b1) throw new MyException("自定义异常: 相除的结果小于1");
else return a/b;
}
public static void main(String[] args) {
MyExceptionTest me = new MyExceptionTest();
System.out.print("请输入第一个数:");
int a = me.scan();
System.out.print("请输入第二个数:");
int b = me.scan();
try{
System.out.println("相除的结果:" + me.cal(a, b));
}catch(MyException e){
System.out.println(e.getMessage());
}
}
}
第三个:
import java.util.*;
import java.util.Map.Entry;
public class CountCharacter {
private static MapString, Integer map = new LinkedHashMapString, Integer();
private static final int ONE = 1 ; //没有出现过,则设置其出现一次;
private String content = null;
public void scan()
{
Scanner scan = new Scanner(System.in);
content = scan.nextLine();
}
public void count()
{
String [] text = content.split(" ");
for(int i=0; itext.length; i++)
{
if (!map.containsKey(text[i])) {
map.put(text[i], ONE);
} else {
int value = map.get(text[i]);
map.put(text[i], value + 1);
}
}
}
public K, V extends Number MapString, V sortMap(MapString, V map) {
class MyMapM, N {
private M key;
private N value;
private M getKey() {
return key;
}
private void setKey(M key) {
this.key = key;
}
private N getValue() {
return value;
}
private void setValue(N value) {
this.value = value;
}
}
ListMyMapString, V list = new ArrayListMyMapString, V();
for (IteratorString i = map.keySet().iterator(); i.hasNext(); ) {
MyMapString, V my = new MyMapString, V();
String key = i.next();
my.setKey(key);
my.setValue(map.get(key));
list.add(my);
}
Collections.sort(list, new ComparatorMyMapString, V() {
public int compare(MyMapString, V o1, MyMapString, V o2) {
if(o1.getValue().equals(o2.getValue())) {
return o1.getKey().compareTo(o2.getKey());
}else{
return (int)(o2.getValue().doubleValue() - o1.getValue().doubleValue());
}
}
});
MapString, V sortMap = new LinkedHashMapString, V();
for(int i = 0, k = list.size(); i k; i++) {
MyMapString, V my = list.get(i);
sortMap.put(my.getKey(), my.getValue());
}
return sortMap;
}
public static void main(String[] args) {
CountCharacter cc = new CountCharacter();
cc.scan();
cc.count();
MapString, Integer sortMap = cc.sortMap(cc.map);
IteratorEntryString, Integer it = sortMap.entrySet().iterator();
Map.EntryString,Integer entry = null;
int i=0;
while(it.hasNext() i10) {//去前面10个
i++;
entry = (EntryString,Integer) it.next();
System.out.println(entry.getKey() + " -- " + entry.getValue());
}
}
}
第四个:
import java.util.Scanner;
public class IntegerShape{
public static void main(String[] args){
double a = 0;
int b = 0;
Scanner in = null;
do{
try{
System.out.print("请输入一个的数:");
in=new Scanner(System.in);
a=in.nextFloat();
break;
}catch(Exception ne){
System.out.println("输入数据错误!");
}
}while(true);
do{
try{
System.out.print("请输入显示的行数:");
in=new Scanner(System.in);
b=in.nextInt();
break;
}catch(Exception ne){
System.out.println("输入数据错误!");
}
}while(true);
for(int i=b;i0;i--)
{
System.out.println(a);
a=a/2;
}
}
}
第五个:
import java.util.Scanner;
import java.util.Vector;
public class MyVector {
private VectorString vectorStr = new VectorString(); //用来方单词
private VectorInteger vectorInt = new VectorInteger();//用来记录对应下标的单词的个数
private static final int ONE = 1 ; //没有出现过,则设置其出现一次;
private String content = null;
public void scan()
{
Scanner scan = new Scanner(System.in);
content = scan.nextLine();
}
public void countWord()
{
int index = -1;
String [] text = content.split(" ");
for(int i=0; itext.length; i++)
{
if(vectorStr.contains(text[i])){ //若次单词已经存在与vector中则只要修改对应的个数
index = vectorStr.indexOf(text[i]);
int value = vectorInt.get(index)+1;
vectorInt.setElementAt(value, index);
}
else{//若不存在则添加该单词,同时要初始化对应下标的单词的个数
vectorStr.add(text[i]);
vectorInt.add(ONE);
}
}
System.out.println(vectorStr);
}
public void display()
{
for(int i=0; ivectorStr.size(); i++)
{
System.out.println(vectorStr.get(i) + "-" + vectorInt.get(i));
}
}
public static void main(String[] args) {
MyVector mv = new MyVector();
mv.scan();
mv.countWord();
mv.display();
}
}
最后一个了不容易!考虑加点分吧!
import java.util.*;
public class Exp {
private int integer;
private boolean bool = false;
private StackInteger stack = new StackInteger();
public void scan()
{
System.out.print("输入一个整数:");
Scanner scan = new Scanner(System.in);
while(true){
try{
integer = scan.nextInt();
break;
}catch(Exception e){
System.out.println("输入错误!");
}
}
}
public void operation()
{
for(int i=1; iinteger; i++)
{
int sum = 0;
for(int j = i; j=integer; j++)
{
stack.push(j);
sum += j;
if(sum == integer)
{
int k = 0, n = stack.size();
bool = true;
System.out.print(integer + " = ");
while(!stack.empty())
{
k++;
if(kn)
{
System.out.print(stack.pop().intValue() + " + ");
}
else
{
System.out.print(stack.pop().intValue() + "\n");
}
}
stack.clear();
}else if(sum integer){
stack.clear();
}
}
}
if(!bool)
System.out.println("该整数没有连续正整数序列!");
}
public static void main(String[] args) {
Exp e = new Exp();
e.scan();
e.operation();
}
}
以上运行过了可以!
关于java简单程序题和java简答题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。