首页 > 编程知识 正文

编写一个程序用java(编写一个程序用于水果店售货员结账)

时间:2023-11-27 14:11:05 阅读:309529 作者:WXJN

本文目录一览:

  • 1、用JAVA编写一个程序,要求如下:
  • 2、用Java编写一个程序,要求如下:
  • 3、根据以下任务要求,编写Java应用程序?
  • 4、用JAVA语言编写一个程序,要求如下:
  • 5、如何编写java程序

用JAVA编写一个程序,要求如下:

实现代码如下:

Student类:

public class Student {

private String name;

private String sex;

private int age;

private double chinese;

private double math;

private double english;

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 getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public double getChinese() {

return chinese;

}

public void setChinese(double chinese) {

this.chinese = chinese;

}

public double getMath() {

return math;

}

public void setMath(double math) {

this.math = math;

}

public double getEnglish() {

return english;

}

public void setEnglish(double english) {

this.english = english;

}

}

-----------------------------------------------------------------

StudentTest类:(测试类)

import java.util.Scanner;

public class StudentTest {

public static void main(String[] args) {

Student student = new Student();

Scanner sc = new Scanner(System.in);

System.out.println("请输入姓名:");

student.setName(sc.next());

System.out.println("请输入性别:");

student.setSex(sc.next());

System.out.println("请输入年龄:");

student.setAge(sc.nextInt());

System.out.println("请输入语文成绩、数学成绩、英语成绩:");

student.setChinese(sc.nextDouble());

student.setMath(sc.nextDouble());

student.setEnglish(sc.nextDouble());

Double count = student.getChinese()+ student.getMath()+student.getEnglish();

System.out.println("姓名:"+student.getName()+" 性别:"+student.getSex()+" 年龄:"+student.getAge());

System.out.println("总分:"+count+" 平均分:"+count/3);

}

}

运行结果为:

用Java编写一个程序,要求如下:

1.内部使用 C 的 longjmp 机制让出一个协程。 因此,如果一个 C 函数 foo 调用了一个 API 函数, 而这个 API 函数让出了(直接或间接调用了让出函数)。 由于 longjmp 会移除 C 栈的栈帧, Lua 就无法返回到 foo 里了。

2.为了回避这类问题, 碰到 API 调用中调用让出时,除了那些抛出错误的 API 外,还提供了三个函数: lua_yieldk, lua_callk,和 lua_pcallk 。 它们在让出发生时,可以从传入的 延续函数 (名为 k 的参数)继续运行。

3.我们需要预设一些术语来解释延续点。 对于从 Lua 中调用的 C 函数,我们称之为 原函数。 从这个原函数中调用的上面所述的三个 C API 函数我们称之为 被调函数。 被调函数可以使当前线程让出。 (让出发生在被调函数是 lua_yieldk, 或传入 lua_callk 或 lua_pcallk 的函数调用了让出时。)

4.假设正在运行的线程在执行被调函数时让出。 当再次延续这条线程,它希望继续被调函数的运行。 然而,被调函数不可能返回到原函数中。 这是因为之前的让出操作破坏了 C 栈的栈帧。 作为替代品,Lua 调用那个作为被调函数参数给出的 延续函数 。 正如其名,延续函数将延续原函数的任务。

5.注意这里那个额外的显式的对延续函数的调用:Lua 仅在需要时,这可能是由错误导致的也可能是发生了让出而需要继续运行,才会调用延续函数。 如果没有发生过任何让出,调用的函数正常返回, 那么 lua_pcallk (以及 lua_callk)也会正常返回。 (当然,这个例子中你也可以不在之后调用延续函数, 而是在原函数的调用后直接写上需要做的工作。)

6.Lua 会把延续函数看作原函数。 延续函数将接收到和原函数相同的 Lua 栈,其接收到的 lua 状态也和 被调函数若返回后应该有的状态一致。 (例如, lua_callk 调用之后, 栈中之前压入的函数和调用参数都被调用产生的返回值所替代。) 这时也有相同的上值。 等到它返回的时候,Lua 会将其看待成原函数的返回去操作。

7.我们需要预设一些术语来解释延续点。 对于从 Lua 中调用的 C 函数,我们称之为 原函数。 从这个原函数中调用的上面所述的三个 C API 函数我们称之为 被调函数。 被调函数可以使当前线程让出。 (让出发生在被调函数是 lua_yieldk, 或传入 lua_callk 或 lua_pcallk 的函数调用了让出时。)

8.假设正在运行的线程在执行被调函数时让出。 当再次延续这条线程,它希望继续被调函数的运行。 然而,被调函数不可能返回到原函数中。 这是因为之前的让出操作破坏了 C 栈的栈帧。 作为替代品,Lua 调用那个作为被调函数参数给出的 延续函数 。 正如其名,延续函数将延续原函数的任务。

希望能帮到你,谢谢!

根据以下任务要求,编写Java应用程序?

按照题目要求编写的Java程序如下

注意 请使用你的真实姓名和班级替换Test类中

创建Student对象stu时用的"张三"和"20计算机应用01班"

import java.util.Scanner;

class Student{

 private String name,classname;

 private int starnum,scorenum;

 private int[] scores;

 public void setStarNum(int n){

  this.starnum=n;

 }

 public Student(String name,String classname,int scorenum){

  this.name=name;

  this.classname=classname;

  this.scorenum=scorenum;

 }

 public String getName(){

  return this.name;

 }

 public void printStar(){

  for(int i=0;istarnum;i++){

   for(int j=0;j2*i+1;j++){

    System.out.print("*");

   }

   System.out.println();

  }

 }

 public void setScore(){

  Scanner sc=new Scanner(System.in);

  scores=new int[scorenum];

  System.out.print("请输入各科成绩:");

  for(int i=0;iscorenum;i++){

   scores[i]=sc.nextInt();

  }

 }

 public void showInfo(){

  System.out.print(name+"同学,你所在的班级是"+classname+",你各科考试成绩分别为:");

  for(int i=0;iscorenum;i++){

   if(i==scorenum-1)

    System.out.print(scores[i]);

   else

    System.out.print(scores[i]+",");

  }

  System.out.println();

 }

 public float getAvg(){

  float sum=0;

  for(int i=0;iscorenum;i++){

   sum=sum+scores[i];

  }

  return sum/scorenum;

 }

}

public class Test{

 public static void main(String[] args){

  Student stu=new Student("张三","20计算机应用01班",5);

  stu.setStarNum(4);

  stu.printStar();

  stu.setScore();

  stu.showInfo();

  if(stu.getAvg()60){

   System.out.println(stu.getName()+"是不合格学生");

  }else{

   System.out.println(stu.getName()+"是个合格学生");

  }

 }

}

用JAVA语言编写一个程序,要求如下:

import java.util.Random;

import java.util.Scanner;

public class T {

public static void main(String[] args) throws Exception {

Scanner in = new Scanner(System.in);

int difficulty;//难度

int mode;//运算类型

int answer;//答案

int amount;//挑战题目数量

int score = 0;//得分

System.out.println("请输入难度(1:一位数、2:两位数、3:三位数):");

difficulty = in.nextInt();

System.out.println("请输入运算类型(1:加、2:减、3:乘、4:除):");

mode = in.nextInt();

System.out.println("请输入想要挑战的题目数量:");

amount = in.nextInt();

Random random = new Random();

for (int i = 0; i amount; i++) {

if (difficulty == 1) {

if (mode == 1) {

int x = random.nextInt(10);

int y = random.nextInt(10);

System.out.println("第" + i + "题:");

System.out.print(x + " + " + y + " = ");

answer = in.nextInt();

if (answer == (x + y)) {

System.out.println("答对了n");

score++;

} else {

System.out.println("答错了,答案是:" + (x + y) + "n");

}

} else if (mode == 2) {

int x = random.nextInt(10);

int y = random.nextInt(10);

System.out.println("第" + i + "题:");

System.out.print(x + " - " + y + " = ");

answer = in.nextInt();

if (answer == (x - y)) {

System.out.println("答对了n");

score++;

} else {

System.out.println("答错了,答案是:" + (x - y) + "n");

}

} else if (mode == 3) {//乘法

} else if (mode == 4) {//除法 考虑小数的问题

} else {

throw new Exception("运算类型输入值不合法");

}

} else if (difficulty == 2) {

if (mode == 1) {

int x = random.nextInt(100);

int y = random.nextInt(100);

System.out.println("第" + i + "题:");

System.out.print(x + " + " + y + " = ");

answer = in.nextInt();

if (answer == (x + y)) {

System.out.println("答对了n");

score++;

} else {

System.out.println("答错了,答案是:" + (x + y) + "n");

}

} else if (mode == 2) {

} else if (mode == 3) {//乘法

} else if (mode == 4) {//除法 考虑小数的问题

} else {

throw new Exception("运算类型输入值不合法");

}

} else if (difficulty == 3) {

if (mode == 1) {

int x = random.nextInt(1000);

int y = random.nextInt(1000);

System.out.println("第" + i + "题:");

System.out.print(x + " + " + y + " = ");

answer = in.nextInt();

if (answer == (x + y)) {

System.out.println("答对了n");

score++;

} else {

System.out.println("答错了,答案是:" + (x + y) + "n");

}

} else if (mode == 2) {

} else if (mode == 3) {//乘法

} else if (mode == 4) {//除法 考虑小数的问题

} else {

throw new Exception("运算类型输入值不合法");

}

} else {

throw new Exception("难度输入值不合法");

}

}

System.out.println("挑战结束,您的分数为:" + score);

}

}

我就只举了加法的例子,其他运算的写法都是类似的,你照葫芦画瓢即可

运行结果:

如何编写java程序

新建java第一种方法:

1。首先右击鼠标,新建一个文本文档。

2。更改文本文档的后缀名为

.java

。注意文件名必须和类名相同。

如果你的电脑没有显示后缀名的话需要对电脑进行一定的设置。

3。打开计算机——组织——文件夹选项——查看——将隐藏文件夹选项取消勾选即可。

建议将这个选项取消勾选,有很多的恶意程序会侵入我们的电脑,取消勾选可以很好的查看病毒等文件的类型。

4。右击建好的文档,选择编辑即可进行java的编写。

在编写的时候我们有一定的格式,java语言的格式是为了我们日后更改程序方便而规定的,我们一定要遵循这种格式编写。

第二种方法:

1直接新建一个记事本文档,然后选择保存。

注意保存时额位置,不要等到保存完却找不到文件到哪了。

2在选择保存的窗口中输入后缀“.java”,然后保存即可。注意文件名必须和类名相同。

3同样右击文件,选择编辑即可在文件中进行编辑了。

前两个大括号之间回车,第二对大括号有一个间隔,直接按TAb键即可。

测试运行java程序:

打开运行命令,也可以直接在开菜单中输入CMD调用命令窗口。

你的文件保存在哪里就将你的文件目录输入到命令提示符窗口中,例如我的是D盘根目录,所以我输入“d:”。

编译java文件,生成class文件:

在命令提示符中输入“javac

文件名.java”,我的文件名为A,所以输入javac

A.java然后回车,等待编译。

运行文件:

继续输入“java

文件名”然后回车就可以运行我们写好的文件了。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。