如何在Java中获取和打印字符串

时间:2018-07-29 11:08:26

标签: java

我可以在Java中获取并打印整数值,但是我对如何获取并打印字符串感到困惑。有人可以帮助

import React, { Component } from "react";
import { Map, InfoWindow, Marker, GoogleApiWrapper } from "google-maps-react";
import "../Body/Map.css";
import marker_icon from "../../img/marker_icon.png";
import hover_icon from "../../img/hover_icon.png";
import { Grid, Row, Col } from "react-bootstrap";
/*global google*/

export class MapContainer extends Component {
  constructor(props) {
    super(props);

    this.state = {
      showingInfoWindow: false,
      activeMarker: {},
      selectedPlace: {}
    };
  }

  onMarkerClick = (props, marker, e) => {
    this.setState({
      selectedPlace: props,
      activeMarker: marker,
      showingInfoWindow: true
    });
  };

  onMapClicked = props => {
    if (this.state.showingInfoWindow) {
      this.setState({
        showingInfoWindow: false,
        activeMarker: null
      });
    }
  };

  addMarker = (mapProps, map) => {
    var marker = new google.maps.Marker({
      position: {},
      map: map
    });
  };

  render() {
    const google = window.google;
    const data = this.props.data;

    return (
      <div className="map-container">
        <Map
          google={this.props.google}
          className={"map"}
          zoom={1}
          onClick={this.onMapClicked}
          onReady={this.addMarker}
        >
          {data.map(item => (
            <Marker
              key={item.id}
              title={item.name}
              name={item.name}
              position={{ lat: item.lat, lng: item.lng }}
              onClick={this.onMarkerClick}
            />
          ))}
          <InfoWindow
            marker={this.state.activeMarker}
            visible={this.state.showingInfoWindow}
          >
            <div className="info">
              <h1>{this.state.selectedPlace.name}</h1>
            </div>
          </InfoWindow>
        </Map>
      </div>
    );
  }
}

export default GoogleApiWrapper({
  apiKey: "AIzaSyDLgdweTnvhPnUE5yzdxcMeK876cFtMaSk"
})(MapContainer);

程序输出

package hello;

import java.util.Scanner;

public class hello {

    public static void main(String[] args) {

        int integer;

        System.out.println("Please Enter Integer");

        Scanner sc = new Scanner(System.in);

        integer = sc.nextInt();

        sc.close();


        System.out.println("you entered : " +integer);


    }
}

我被困在这个程序中。我不明白如何获取字符串并在屏幕上打印

Please Enter Integer
5
you entered : 5

5 个答案:

答案 0 :(得分:2)

您需要将类型值nameint更改为String。并将sc.nextInt()替换为sc.nextLine()sc.next()

示例

public static void main(String[] args) {

    String name;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter your name");


    name = sc.nextLine();

    sc.close();

    System.out.println("Your name " + name);

}

答案 1 :(得分:1)

首先,您需要进行两项更改

  1. 将名称int的数据类型更改为String
  2. 使用sc.nextInt()sc.next()

答案 2 :(得分:0)

使用sc.nextLine()读取字符串输入 要么 sc.next()(但这只会在遇到空格之前读取一个单词)

您也可以为此使用InputStreamReader

例如

BufferedReader br =新的BufferedReader(新的InputStreamReader(System.in()));

字符串输入= br.readLine();

答案 3 :(得分:0)

name = sc.nextInt();不适用于字符串,仅适用于整数,应改用sc.nextline。 另外,由于其他类型的变量,您还必须将int name更改为String name。

您的代码应如下所示:

 import java.util.Scanner;

public class hello {

    public static void main(String[] args) {

        String name;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter your name");


        name = sc.nextLine(); 

         sc.close();

         System.out.println("Your name"+name);


    }
}

答案 4 :(得分:0)

将int名称更改为字符串名称并使用sc.nextLine()

import java.util.Scanner;

public class hello {

    public static void main(String[] args) {

        String name;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter your name");


        name = sc.nextLine(); 

         sc.close();

         System.out.println("Your name"+name);


    }
}