因此,我需要能够调用seqs = ['GTACGCCTTCTTCGGATTGTTAGCCCCTTTTGTTGGGTTACTGCT', 'CCGTGGTTGTTTGTTGAGCTGGGGCTTGTTGCGTGATGCAGCAT', 'TGGGAATTTTGGAATGGGGGAAACCCTGATCAGCCTCCCGCGTG', 'GGGTGTGTGAAGAAGGCCTTCGGGTTGTAAAGCACTTTCAGCGG', 'GATGAAGGCCTTCGGGTTGTAAAGTACTTTTGGCAGAGAAGAAA']
indexa = 0
for i in seqs: # iterates through each string in list seqs
if len(i) < ((max(len(i) for i in seqs))): # Identifies any string which is
shorter than the longest string
seqs[indexa].append('N') # Appends all shorter strings with 'N' to make
the same length. (**DOESN'T WORK** How to
call the index of the current iteration?)
indexa += 1 # Count for append
print(seqs)
largest_matches = []
seq_pos = 0
for i in seqs: # iterates through each string
matches = {} # Creates dictionary to keep track of counts for each
comparison
for j in seqs[i]: # Iterates through each character in the current
string
for s in seqs[i+1:]: # Compares character at index j in the
current string i to *only* subsequent
strings, at the same index value
if j == seqs[s][j]: # Compares the current character (j)
in the current string (i) to the
corresponding character in subsequent
strings (s)(**NOT SURE IF THIS
WORKS**)
matches[seqs[i][j], seqs[s][j]] += 1 # Counts "matches"
at each index
**NEED CODE HERE FOR SUMMING ALL MATCHES AT EACH INDEX FOR EACH PAIR. FOLLOWING CODE SHOULD WORK IF A DICTIONARY CALLED MATCH_SUMS IS CREATED PRIOR (I'M NOT SURE HOW TO DO THIS.)**
for l, o in match_sums.items(): # iterates through summed match counts
for each pairing
if o == max(o) # checks to see if current value, o (matches), at
key l (string pair) has the largest or tied for the
largest number of matches
largest_match.append(l, o)
seq_pos += 1
if seq_pos == (len(seqs)):
break # ends loop at sequence 5, or last sequence, as all pairings have
then been evaluated. (4 to 5 being the last pairing here)
'''
方法并按预期方式运行它,但是似乎无法弄清楚如何使其运行。任何帮助将不胜感激。如果您还对如何增强我的代码有任何意见,这将非常有用,在此先感谢您!
PS。我不能使该方法静态化,我必须想出一种在方法不静态的情况下调用它的方法。
.addClients()
<---------------------------------------------- --->
import java.util.Scanner;
public class BankApp {
private SavingsAccount[] clients;
public BankApp() {
Scanner bb = new Scanner(System.in);
System.out.println("How many clients do you want on the
system?");
int numofclients = bb.nextInt();
this.clients = new SavingsAccount[numofclients];
clients.addClients();
}
public static void main(String args[]) {
BankApp ba = new BankApp();
}
}
我希望import java.util.*;
public class SavingsAccount {
private SavingsAccount[] clients;
private double Balance;
private String ID, Name;
public SavingsAccount(String startingID, String startingName, double
startingBalance) {
this.Balance = startingBalance;
this.ID = startingID;
this.Name = startingName;
}
public void addClients() {
int i = 0;
while (i < clients.length) {
System.out.println("What is ID of user " + (i + 1));
this.clients[i] = new SavingsAccount("DF01", "Dandy",
10.00);
i++;
}
}
public double getBalance() {
return Balance;
}
public String getName() {
return Name;
}
public String getID() {
return ID;
}
}
会填充addClients()
。但是clients[]
必须先运行。
答案 0 :(得分:0)
考虑代码的这一部分:
this.clients = new SavingsAccount[numofclients];
clients.addClients();
在这里,您的clients
变量指向 SavingsAccounts数组,并且每个SavingsAccount
都有自己的addClients()
方法。
所以,我相信您想要的是遍历数组的元素?
for(SavingsAccount client : clients) {
client.addClients();
}
但是,正如上面的社区Wiki所提到的,这看起来像是一个很奇怪的设计。为什么会有SavingsAccount
类数组作为SavingsAccount
类的字段?
答案 1 :(得分:-1)
您担心我怕错了。您的问题不是如何调用addClients(...)
,而是应该从头开始正确地构造程序,因为当前的设计已损坏。
SavingsAccount类应该关注单个储蓄帐户的状态(变量)和行为(方法),仅此而已。它不应保存一个SavingsAccount数组,也不应在其中包含任何用户交互(UI)代码,例如println语句,除非它们是临时的且用于调试目的。
相反,BankApp类应该保存实例数组,并且可能保存addClient(...)
方法。
另一个驱动程序类应包含main方法,并可能控制所有用户交互(ui)。
重新研究您的要求,然后从头开始重新设计。 我也在猜测您可能需要Client类吗?我不能肯定地说
答案 2 :(得分:-1)
您需要做的是获取SavingsAccount类的SavingsAccount数组 ,因为它根本不属于那里。同样,SavingsAccount应该只涉及一个帐户:
public class SavingsAccount {
private String id;
private String name;
private double balance;
public SavingsAccount(String iD, String name, double balance) {
this.id = iD;
this.name = name;
this.balance = balance;
}
// .... getters and setters here
public void deposit(double funds) {
balance += funds;
}
public double withdrawal(double funds) {
// check if the balance is adequate before withdrawing
// .....
}
}
BankApp或更简单的Bank.java类应包含数组和 addClient方法,并且该方法应接受单个SavingsAccount对象。同样,该方法不属于SavingsAccount
import java.util.Scanner;
public class BankApp {
private SavingsAccount[] clients;
private int accountIndex = 0;
public BankApp(int clientCount) {
clients = new SavingsAccount[clientCount];
}
// keep it simple -- it simply adds a client if there is room in the array
public void addClient(SavingsAccount account) {
if (accountIndex >= clients.length) {
// you've got a problem -- throw an exception
} else {
clients[accountIndex] = account;
accountIndex++;
}
}
// methods to print a client, to print all clients, to remove a client,.....
然后在BankApp类(或其他地方)中,有一个主要方法驱动您的程序:
public static void main(String[] args) {
// User interface goes here
Scanner scanner = new Scanner(System.in);
System.out.print("How many clients do you want on the system? ");
int numofclients = scanner.nextInt();
BankApp bank = new BankApp(numofclients);
for (int i = 0; i < numofclients; i++) {
// get id, name, balance of each client
// create anew SavingsAccount object with this info
// call bank.addClient(account); to add it to the bank
}
scanner.close();
}
}