我尝试对代码进行排序并交换不按字母顺序排列的标题,然后显示结果,但没有成功。没有错误,但由于数组未使用,因此不会显示电影标题。不知道我做错了什么。我尝试过谷歌搜索,但就我而言,我不知道我错过了什么。
主要
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package movieapp;
/**
*
* @author T
*/
import java.util.*;
public class Movieapp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
moviearray arrMovie;
arrMovie = new moviearray(5);
int nElems = 5;
String movieArr[] = {"Lion king", "Avengers", "Interstellar", "Batman", "13 Hours"};
int ratingArr[] = {4, 5, 8, 10, 3};
arrMovie.sort();
arrMovie.display();
}
}
电影课
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package movieapp;
/**
*
* @author T
*/
public class movie {
String movie;
int rating;
movie(String m, int r){
this.movie = m;
this.rating = r;
}
public void info(){
System.out.println("The movie " + this.movie + " is rated a " + this.rating);
}
}
电影数组类
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package movieapp;
/**
*
* @author T
*/
public class moviearray {
private movie[] arrMovie;
private int nElems = 0;
moviearray(int size) {
arrMovie = new movie[size];
nElems = 0;
}
public void sort() {
int out;
int in;
for (out = nElems - 1; out > 1; out--) {
for (in = 0; in < out; in++) {
if (arrMovie[in].movie.compareToIgnoreCase(arrMovie[in + 1].movie) > 0) {
swap(in, in + 1);
}
}
}
}
public void swap(int x, int y) {
movie temp = arrMovie[x];
arrMovie[x] = arrMovie[y];
arrMovie[y] = temp;
}
public void display() {
for (int i = 0; i < nElems; i++) {
arrMovie[i].info();
}
}
}
编辑Movieapp Class和Moviearray,添加一组并获取
电影应用
*/
package movieapp;
/**
*
* @author T
*/
import java.util.*;
public class Movieapp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
moviearray arrMovie;
arrMovie = new moviearray(5);
int nElems = 5;
String movieArr[] = {"Lion king", "Avengers", "Interstellar", "Batman", "13 Hours"};
int ratingArr[] = {4, 5, 8, 10, 3};
for(int i = 0; i < nElems; i++){
arrMovie.setElement(i, movieArr[i], ratingArr[i]);
}
for(int i = 0; i < nElems; i++){
arrMovie.getElement(i).info();
}
}
}
电影数组
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package movieapp;
/**
*
* @author T
*/
public class moviearray {
public movie[] arrMovie;
public int nElems = 0;
moviearray(int size) {
arrMovie = new movie[size];
nElems = 0;
}
public void setElement(int index, String movie, int rating){
arrMovie[index]= new movie(movie, rating);
}
public movie getElement(int index){
return arrMovie[index];
}
public void sort() {
int out;
int in;
for (out = nElems - 1; out > 1; out--) {
for (in = 0; in < out; in++) {
if (arrMovie[in].movie.compareToIgnoreCase(arrMovie[in + 1].movie) > 0) {
swap(in, in + 1);
}
}
}
}
public void swap(int x, int y) {
movie temp = arrMovie[x];
arrMovie[x] = arrMovie[y];
arrMovie[y] = temp;
}
public void display() {
for (int i = 0; i < nElems; i++) {
arrMovie[i].info();
}
}
}
输出:
现在只需要按字母顺序排列即可。
最佳答案
你做错了。你创建了你的对象,但你没有给这个对象分配值。
moviearray arrMovie;
arrMovie = new moviearray(5);
arrMovie.nElems = 5;
arrMovie.arrMovie = {
new Movie("Lion king",4),
new Movie("Avengers",5),
new Movie("Interstellar",8),
new Movie("Batman",10),
new Movie("13 hours",3)
};
您必须将 nElems 和 arrMovie 更改为公共(public)或为这些字段添加 setter 。
我认为你的设计很糟糕,但这个改变解决了你的问题
关于java - 尝试按字母顺序对我的电影标题进行排序,然后将它们与我的评级数组中的评级之一一起显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58419407/