gpt4 book ai didi

java - 更改对象字段中的变量值

转载 作者:太空宇宙 更新时间:2023-11-04 14:41:45 26 4
gpt4 key购买 nike

以下是我的代码:

package com.collegeselector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;



public class CollegeList extends ListActivity {

ArrayList<CollegeItem> collegeLists=new ArrayList<CollegeItem>();
ArrayList<String> nameList = new ArrayList<String>();

Comparator<CollegeItem> compare = new Comparator<CollegeItem>(){
public int compare(CollegeItem a, CollegeItem b){
return Double.compare(a.getScoreDistance(), b.getScoreDistance());
}
};


CollegeItem michigan = new CollegeItem(3.79,30,2020,"University of Michigan","Ann Arbor, Michigan",true,false,true,false,true,true,true,true,true,true,true,true);
CollegeItem berkeley = new CollegeItem(3.84,30,2040,"University of California Berkeley","Berkeley, California",false,false,true,true,true,true,true,true,true,true,true,true);
CollegeItem stanford = new CollegeItem(3.96,33,2215,"Stanford University","Stanford, California",true,false,true,true,true,true,true,true,true,true,true,true);
CollegeItem mit = new CollegeItem(3.92,33,2206,"Massachusetts Institute of Technology","Cambridge,Massachusetts",true,false,true,true,true,true,true,true,true,true,true,true);
CollegeItem cit = new CollegeItem(3.95,34,2300,"California Institute of Technology","Pasadena,California",true,false,false,true,false,true,true,true,false,false,false,true);
CollegeItem git = new CollegeItem(3.9,30,2010,"Georgia Institute of Technology","Atlanta,Georgia",true,false,true,true,true,true,true,false,true,true,true,true);
CollegeItem uiuc = new CollegeItem(3.4,28,1975,"University of Illinois at Urbana-Champaign","Champaign,Illinois",true,true,false,false,true,true,true,true,true,false,true,true);
CollegeItem carnegie = new CollegeItem(3.69,31,2100,"Carnegie Mellon University","Pittsburgh, Pennsylvania",false,false,false,false,false,true,true,false,true,false,true,false);
CollegeItem cornell = new CollegeItem(3.87,31,2150,"Cornell Univeristy","Ithaca, New York",false,true,false,true,true,true,true,false,true,false,true,true);
CollegeItem princeton = new CollegeItem(3.87,33,2260,"Princeton University","Princeton, New Jersey",true,false,false,true,false,false,false,false,false,false,false,false);
CollegeItem purdue = new CollegeItem(3.7,27,1749,"Purdue University","West Lafayette, Indiana",true,true,false,false,true,false,true,false,false,true,false,true);
CollegeItem utaustin = new CollegeItem(3.72,28,1854,"University of Texas at Austin","Austin, Texas",true,false,false,true,true,true,false,false,true,false,false,true);
CollegeItem northwestern = new CollegeItem(3.8,32,2155,"Northwestern University","Evanston, Illinois",false,false,false,false,false,false,false,false,false,true,true,false);
CollegeItem wisconsin = new CollegeItem(3.84,28,1905,"University of Wisconsin-Madison","Madison, Wisconsin",false,false,false,true,false,false,false,false,false,true,false,false);
CollegeItem calpoly = new CollegeItem(3.87,28,1838,"California Polytechnic State University, San Luis Obispo","San Luis Obispo, California",false,false,false,false,false,false,false,false,false,false,false,false);
CollegeItem johnshopkins = new CollegeItem(3.72,32,2103,"Johns Hopkins University","Baltimore, Maryland",false,false,true,false,false,false,false,false,true,false,false,false);
CollegeItem pennstate = new CollegeItem(3.59,27,1778,"Pennsylvania State University-University Park","University Park, Pennsylvania",false,false,false,false,false,false,false,true,false,true,true,false);
CollegeItem rice = new CollegeItem(3.87,32,2155,"Rice University","Houston, Texas",false,false,true,false,false,false,false,false,false,false,false,false);
CollegeItem texasam = new CollegeItem(3.6,27,1755,"Texas A&M University","College Station, Texas",false,true,false,false,false,false,false,false,false,true,false,false);
CollegeItem vtech = new CollegeItem(3.77,28,1823,"Virginia Tech","Blacksburg, Virginia",false,false,false,false,true,false,false,true,false,true,false,false);







@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
collegeLists.add(michigan);collegeLists.add(purdue);
collegeLists.add(berkeley);collegeLists.add(utaustin);
collegeLists.add(stanford);collegeLists.add(northwestern);
collegeLists.add(mit);collegeLists.add(wisconsin);
collegeLists.add(cit);collegeLists.add(calpoly);
collegeLists.add(git);collegeLists.add(johnshopkins);
collegeLists.add(uiuc);collegeLists.add(pennstate);
collegeLists.add(carnegie);collegeLists.add(rice);
collegeLists.add(cornell);collegeLists.add(texasam);
collegeLists.add(princeton);collegeLists.add(vtech);



Collections.sort(collegeLists, compare);

for(CollegeItem collegeList : collegeLists){
nameList.add(collegeList.getName());
}

setListAdapter(new ArrayAdapter<String>(CollegeList.this, android.R.layout.simple_list_item_1, nameList));



}
private class CollegeItem {
private double gpa;
private int act;
private int sat;
private String name;
private String location;
private double score;
private boolean match;
private double scoreDistance;

private boolean uaero, uagri, ubio, uchem, ucivil, ucomp, uelec, uphys, uenvi, uindus, umate, umech;

public CollegeItem(double gpa, int act, int sat, String name, String location, boolean uaero, boolean uagri, boolean ubio, boolean uchem,
boolean ucivil, boolean ucomp, boolean uelec, boolean uphys, boolean uenvi, boolean uindus, boolean umate, boolean umech){
this.gpa = gpa;
this.act = act;
this.sat = sat;
this.name = name;
this.location = location;
this.uaero = uaero;
this.uagri = uagri;
this.ubio = ubio;
this.uchem = uchem;
this.ucivil = ucivil;
this.ucomp = ucomp;
this.uelec = uelec;
this.uphys = uphys;
this.uenvi = uenvi;
this.uindus = uindus;
this.umate = umate;
this.umech = umech;

if(act/36.0>sat/2400.0){
this.score = 0.6*gpa*25.0+0.4*(act/36.0)*100.0;
}else{
this.score = 0.6*gpa*25.0+0.4*(sat/2400.0)*100.0;
}
scoreDistance = Math.abs(this.score-MainActivity.scoreDouble)/MainActivity.scoreDouble;
if(uaero&&ListOfMajors.aerospace)
match = true;
if(uagri&&ListOfMajors.agricultural)
match = true;
if(ubio&&ListOfMajors.biomed)
match = true;
if(uchem&&ListOfMajors.chem)
match = true;
if(ucivil&&ListOfMajors.civil)
match = true;
if(ucomp&&ListOfMajors.computer)
match = true;
if(uelec&&ListOfMajors.electrical)
match = true;
if(uphys&&ListOfMajors.physics)
match = true;
if(uenvi&&ListOfMajors.environment)
match = true;
if(uindus&&ListOfMajors.industrial)
match = true;
if(umate&&ListOfMajors.materials)
match = true;
if(umech&&ListOfMajors.mechanical)
match = true;
}

public String getName(){
return this.name;
}
public double getScoreDistance(){
return this.scoreDistance;
}

list Activity

public class ListOfMajors extends Activity {

boolean[] mItemState;
public static boolean aerospace, agricultural, biomed, chem, civil, computer, electrical, physics, environment, industrial, materials, mechanical;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.majorslist);
ListView mylist = (ListView) findViewById(R.id.majorslist);
final String[] list={"Aerospace Engineering","Agricultural Engineering",
"Biomedical Engineering","Chemical Engineering","Civil Engineering",
"Computer Engineering","Electrical Engineering","Engineering Physics",
"Environmental Engineering","Industrial Engineering",
"Materials Engineering","Mechanical Engineering"};
mItemState = new boolean[list.length];
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListOfMajors.this,android.R.layout.simple_list_item_multiple_choice,list);
mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Toggle the state
mItemState[position] = !mItemState[position];
if (mItemState[position])
switch(position){
case 0: aerospace = true;
case 1: agricultural = true;
case 2: biomed = true;
case 3: chem = true;
case 4: civil = true;
case 5: computer = true;
case 6: electrical = true;
case 7: physics = true;
case 8: environment = true;
case 9: industrial = true;
case 10: materials = true;
case 11: mechanical = true;
}

else{
switch(position){
case 0: aerospace = false;
case 1: agricultural = false;
case 2: biomed = false;
case 3: chem = false;
case 4: civil = false;
case 5: computer = false;
case 6: electrical = false;
case 7: physics = false;
case 8: environment = false;
case 9: industrial = false;
case 10: materials = false;
case 11: mechanical = false;
}
}

}
});

mylist.setAdapter(adapter);
}

}

uaero 和 uagri 等变量是对象的 boolean 字段。我在这个类有很多对象。 ListOfMajors.aerospace 和 ListOfMajors.agriculture 等变量是来自另一个类的 boolean 值,如果单击复选框,这些变量将设置为 true。所有这些变量对都相互对应,并且如果其他两个变量都为 true,则希望变量匹配设置为 true。截至目前,代码似乎无法正常工作,因为匹配似乎并未更改为 true。我应该怎么做才能让它发挥作用?

最佳答案

您的案例可能没有按照您的想法进行..因为没有中断,案例位置 0 会将所有变量设置为 true(或 false)

 switch(position){
case 0: aerospace = true; break;
case 1: agricultural = true;break;
case 2: biomed = true;break;
case 3: chem = true;break;
case 4: civil = true;break;
case 5: computer = true;break;
case 6: electrical = true;break;
case 7: physics = true;break;
case 8: environment = true;break;
case 9: industrial = true;break;
case 10: materials = true;break;
case 11: mechanical = true; break;
}

关于java - 更改对象字段中的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24855928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com