当前位置: 首页 > 知识库问答 >
问题:

为变量赋值

沈开畅
2023-03-14
import java.util.Scanner;
public abstract class Person {  
public String firstName ;
public String lastName;
public String emailID;
public long phoneNumber;
Person(){}
public void readInfo() {    
    Scanner sc;
}
public void printInfo() {   
    System.out.println(firstName + lastName + emailID + phoneNumber);
}       
}




public interface Policies { 
double MAX_MARKS = 100;
double MAX_GPA = 4.0;   
public void calculateGPA(double[] marks) ;
}




import java.util.Scanner;
public class Student extends Person  implements Policies {
public int studentNumber;
public String programName;
public double gpa;
Student(){}
@Override
public void readInfo() {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter program name: ");
    programName = sc.next();
    System.out.print("Enter student number: ");
    studentNumber = sc.nextInt();
    System.out.print("Enter first name: ");
    firstName = sc.next();
    System.out.print("Enter last name: ");
    lastName = sc.next();
    System.out.print("Enter email ID: ");
    emailID = sc.next();
    System.out.print("Enter phone number: ");
    phoneNumber = sc.nextLong();
    readMarks();
   }
    private void readMarks() {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter number of courses: ");
    int numCourses = sc.nextInt();
    double marks[] = new double[numCourses];
    for(int i = 0; i < numCourses; i++) {
        System.out.print("Enter mark " + (i+1) + ":");
        marks[i] = sc.nextDouble();
    }

    calculateGPA(marks);        
   }
   @Override
    public void calculateGPA(double marks[]) {
    double totalMarks =0;
    for(int i=0; i < marks.length; i++)
        totalMarks = totalMarks +marks[i];
    gpa = (totalMarks/marks.length *MAX_MARKS)*MAX_GPA;
  }
  @Override
   public void printInfo() {
    super.printInfo();
    System.out.printf("%25d  | %25s  | %25s  | %25s  | %25s  | %25.2f  |\n", studentNumber 
,programName , firstName, lastName, emailID,  gpa);
}
 }




import java.util.Scanner;
public class FulltimeStudent extends Student {  
double tuitionFees; 
FulltimeStudent(){} 
@Override
public void readInfo() {    
    Scanner sc = new Scanner (System.in);   
    super.readInfo();   
    System.out.println("Enter tuition fees");
    tuitionFees = sc.nextDouble();  
}
@Override
public void printInfo() {   
    super.printInfo();  
    System.out.printf("%25d |\n",tuitionFees);  
}
}




import java.util.Scanner;
public class ParttimeStudent extends Student {  
double courseFeesTotal;
double credits; 
ParttimeStudent(){}
@Override
public void readInfo() {
    Scanner sc = new Scanner (System.in);
    super.readInfo();    
    System.out.println("Enter total course fees:");
    courseFeesTotal = sc.nextDouble(); 
    System.out.println("Enter credit hours:");
    credits = sc.nextDouble();
}
@Override
public void printInfo() {   
    super.printInfo();  
    System.out.printf("%25d | %25d\n",courseFeesTotal, credits);    
}   
}






import java.util.Scanner;
public class College {
String name;
Student [] students;
int numStudents;
College(){}
College(String name, int num){
    this.name = name;
    students = new Student[num];
}
public void printTitle() {
    System.out.println(name + " - List of Students");
    for(int i = 0; i< 40; i++ )
        System.out.print("*");
    System.out.printf("%n%15s  | %15s  | %15s  | %15s  | %15s  | %15s  | %15s  | %15s  |\n", 
 "Program" , "Student#" , "Name" , "Email" , "Phone" , "GPA", "Fees", "Credits");
}
public void ReadStudentDetails() {
    Scanner sc = new Scanner (System.in);
    System.out.println("1 - Fulltime Student \n2 - Parttime Student");
    System.out.print("Enter student type:");
    int type = sc.nextInt();
    if (type == 1) {    
        FulltimeStudent fts = new FulltimeStudent();
        fts.readInfo();
    }
    else if (type == 2) {
        ParttimeStudent pts = new ParttimeStudent();
        pts.readInfo();
    }
    else if (type <1 || type >2) { 
        System.out.println("*** Wrong student type... PLease re-enter ***");
    }
    while (type <1 || type >2) {
        System.out.println("1 - Fulltime Student \n2 - Parttime Student");
        System.out.print("Enter student type:");
        type = sc.nextInt();
        if (type == 1) {    
            FulltimeStudent fts = new FulltimeStudent();
            fts.readInfo();
        }
        else if (type == 2) {
            ParttimeStudent pts = new ParttimeStudent();
            pts.readInfo();
        }
        else if (type <1 || type >2) { 
            System.out.println("*** Wrong student type... PLease re-enter ***");
        }
    }
    numStudents++;
}
public void printStudentDetails() {
    if (numStudents == 0) {
        System.out.println("*** No students to print ***");
    }
    else {
        printTitle();
        for (int i = 0; i < numStudents; i++) {
            if (students[i] != null) 
                students[i].printInfo();
        }
    }
}
}




import java.util.Scanner;
 public class CollegeSystemTest {
public static void main(String[] args) {    
    Scanner sc = new Scanner (System.in);
    System.out.println("Enter name of college:");
    String name = sc.next();
    System.out.println("Enter number of students:");
    int numStudents = sc.nextInt(); 
    College c = new College(name, numStudents);     
    int option;     
    do {
        System.out.println("1. Read student \n2. Print details of all students \n3. Exit 
  \nEnter your option:");
        option = sc.nextInt();
        if (option == 1) {
            c.ReadStudentDetails();     
        }
        else if (option == 2) { 
            //c.printTitle();
            c.printStudentDetails() ;
        }
    }while(option != 3); //a do-while loop is used to display menu items and will keep running 
  until option 3 is chosen
    sc.close(); 
}
 }

共有1个答案

段干昊然
2023-03-14

让我们从printstudentdetails()开始解决这个问题

你说什么都没有输出(大概是...除了标题)。

    for (int i = 0; i < numStudents; i++) {
        if (students[i] != null) 
            students[i].printInfo();
    }

问:为什么那不会打印什么?

    null
Student [] students;
int numStudents;
College(){}

College(String name, int num) {
    this.name = name;
    students = new Student[num];
}
int type = sc.nextInt();
if (type == 1) {    
    FulltimeStudent fts = new FulltimeStudent();
    fts.readInfo();
}
...

答:你把信息读进去,然后....没什么。

问:它如何进入student数组?

A:没有!!

    null
 类似资料:
  • = 赋值操作符(它的左右两边不能有空白符) 不要搞混了 = 和 -eq,-eq 是比赋值操作更高级的测试。注意:等于号(=)根据环境的不同它可能是赋值操作符也可能是一个测试操作符。 例子 4-2. 简单的变量赋值 1 #!/bin/bash 2 # 裸变量 3 4 echo 5 6 # 什么时候变量是“裸”的?比如说,变量名前面没有$? 7 #当变量被赋值

  • 通过函数构造函数创建变量时它们似乎被声明为VAL。由于VAL不能重新赋值(出于某些原因,我需要这样做),我想知道是否有办法在函数之前的一行代码中将变量声明为var,然后通过函数赋值。 请记住,我昨天才开始学习静态编程语言。我对所有替代方案都持开放态度。 公共趣味单打(enemyhealth:Int,enemyattack:Int,enemyname:String) 当我尝试重新分配敌人的健康时,我

  • 主要内容:声明变量,变量赋值Java 语言是强类型(Strongly Typed)语言,强类型包含以下两方面的含义: 所有的变量必须先声明、后使用。 指定类型的变量只能接受类型与之匹配的值。 这意味着每个变量和每个表达式都有一个在编译时就确定的类型。类型限制了一个变量能被赋的值,限制了一个表达式可以产生的值,限制了在这些值上可以进行的操作,并确定了这些操作的含义。 常量和变量是 Java 程序中最基础的两个元素。常量的值是不

  • 我的代码是这样的: 但最后一句话: 总是停止编译说我需要给新变量分配一个返回值?在if语句之前,已经为k分配了一个值。当我把随机k语句放在if语句中时,它似乎是有效的,但这使得它毫无价值,不是吗?编辑器本身没有错误,但是当我编译时,它给了我这个: 线程“main”java中出现异常。lang.IndexOutOfBoundsException:索引:41,大小:36。util。ArrayList。

  • 原文: http://exploringjs.com/impatient-js/ch_variables-assignment.html 下面这些是 JavaScript 声明变量的主要方式: let用来声明可变变量。 const用来声明常量(不可变变量)。 在ES6之前,还有var。但它有些怪癖,所以最好在现代JavaScript程序中避免使用它。你可以在“Speaking JavaScript

  • 问题内容: 在这段代码中,最后,我为c1分配了一个值,但是当我打印它时,我在所有c1字段中都得到了null。我为Cliente类写下了代码。我想打印我给c1的所有值,但我不知道为什么在所有字段中都打印null。我使用调试器遵循代码,直到将所有值分配给新变量的语句为止,一切都正确。 问题答案: 你应该改变这个 至 您正在为方法参数而不是字段分配值。同样适用于构造函数中的所有参数。