问答题

本题程序将字符串sir中的字符a用$符号代替,然后将字符串中第一个$字符前的听有字符去掉,并打印输出最后的str字符串。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
public class basic

public static void main(String[] args)

String str = "Iamagoodboy,andwhataboutyou";
str =______;
int pos =______;
str =______;
System.out.println (str);

【参考答案】

str.replace(’a’,’$’)。
str.indexOf("$")。
str.substring(pos)。

热门 试题

问答题
本题程序是一个Applet应用程序,功能是计算前n个自然数的和,程序中用进度条来表示计算的进程。页面中有两个文本框、两个按钮和一个进度条,在第一个文本框中输入要计算的自然数的个数,单击“开始”按钮则开始进行计算,进度条同步显示计算完成的情况。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。 import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class advance extends Japplet Container container = null; JButton startButton,stopButton; JTextField inputTextField,outputTextField; JProgressBar pBar = null; Timer timer = null; static int sum = 0; static int counter = 0; public void init() container = this.getContentPane(); container.setLayout(new GridLayout(3,1)); Box hboxl = Box.createHorizontalBox(); container.add(hboxl); hboxl.add(Box.createHorizontalGlue()); JLabel labell = new JLabel( 前 ,JLabeI.LEFT); labell.setFont(new Font( Dialog ,Font.PLAIN,15)); hboxl.add(labell); inputTextField = new JTextField( 100 ,4); hboxl.add(inputTextField); JLabel label2 = new JLabel( 个自然数和是 ,JLabel.LEFT); label2.setFont(new Font( Dialog ,Font.PLAIN,15)); hboxl.add(label2); outputTextField = new JTextField(10); hboxl.add(outputTextField); hboxl.add(Box.createHorizontalGlue()); Box hbox2 = Box.createHorizontalBox(); container.add(hbox2); startButton : new JButton( 开始 ); startButton.addActionListener(new ButtonListener()); hbox2.add(Box.createHorizontalGlue()); hbox2.add(startButton); hbox2.add(Box.createHorizontalGlue()); stopButton = new JButton( 结束 ); stopButton.addActionListener(new ButtonListener() ; hbox2.add(Box.createHorizontalGlue()); hbox2.add(stopButton); hbox2.add(Box.createHorizontalGlue()); pBar = new JProgressBar(); pBar.setStringPainted(true); Border border = BorderFactory.createLineBorder(Color.red,2); pBar.setBorder(border); pBar.setBackground(Color.white); pBar.setForeground(Color.blue); pBar.setMinimum(0); pBar.setMaximum(Integer.parseInt(inputTextField.getText())); container.add(pBar); timer = new Timer(0,TimerListener()); class TimerListener implements ActionListener public void actionPerformed(ActionEvent e) if (Integer.parseInt (inputTextField.getText ()) > 0) counter ++; sum = sum+counter; pBar.setValue (counter); outputTextField,setText (Integer.toString (sum)); else outputTextField,setText ( 0 ); if (counter >= Integer.parseInt(inputTextField.getText ())) timer,stop (); class ButtonListener implements ActionListener public void actionPerformed(ActionEvent e) JButton button = e.getSource(); if (button.getText () == 开始 ) outputTextField,setText ( ); if (inputTextField.getText() != ) pBar.setMaximum (Integer.parseInt (inputTextField.getText ()) ); sum = 0 ; counter = 0; timer.begin (); else if (button.getText () == 结束 ) timer,stop (); outputTextField,setText ( ); sum = 0; counter = 0; pBar.setValue(0);